Converting $timestamp to ISO 8601

#1

How can I convert $timestamp to ISO8601 format in a pre-request script?

This page on Stack Overflow has a function to do it, but neither Date() nor toISOString() seem to be available.

#2

Dynamic variables (like $timestamp) cannot be used in the Sandbox. You can only use them in the {{..}} format in the request URL / headers / body.
https://learning.getpostman.com/docs/postman/environments_and_globals/variables/#dynamic-variables.

Try:

var dateIso = new Date().toISOString();

Good reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString

2 Likes
#3

Me too!! I mean… for all ISO date needs, I fall back to the code snippet that @matt posted.

However, if I have to do heavy date lifting, Postman Sandbox has the powerful “moment” library built in - more at https://momentjs.com/

var moment = require('moment');
moment().format();

Docs of moment: http://momentjs.com/docs/#/displaying/as-iso-string/

2 Likes
#4

Thank you. That worked beautifully!

@shamasis, Thank you, I’ll check that when I have some free time.