Skip to content

Instantly share code, notes, and snippets.

@lsloan
Last active September 4, 2023 11:44
Show Gist options
  • Save lsloan/8b986e4ac194dc01de35c017f355e455 to your computer and use it in GitHub Desktop.
Save lsloan/8b986e4ac194dc01de35c017f355e455 to your computer and use it in GitHub Desktop.
Postman current timestamp for UTC as ISO 8601

I wanted to know: How can I get the current timestamp for UTC in ISO 8601 format to appear in the body data of a Postman request?

After reading some of the Postman documentation and online comments, this is the solution (using Postman v5.0.1 for Chrome 58.0.3029.110 on macOS 10.12.5) I used:

  1. In the Builder, while editing a request, click the "Pre-request Script" heading below the URL field.

  2. In the editor field that appears, enter this single line of JavaScript:

    postman.setGlobalVariable('timestampUtcIso8601', (new Date()).toISOString());
  3. Click the "Body" heading below the URL field.

  4. In the editor field that appears, wherever you want the newly formatted timestamp to appear, add the reference {{timestampUtcIso8601}}.

    💡 Remember: Don't use a dollar sign ($) in this variable reference. That's only needed for reserved system variables automatically set by Postman. E.g., the system timestamp (the number of seconds since 00:00:00 UTC, 1 Jan 1970), {{$timestamp}}.

How this works is Postman runs the "Pre-request Script" before each request, which updates the timestampUtcIso8601 global variable with the current time. When the request references the variable, it will be replaced with its most recent value.

As others who had a similar question have written, it can be repetitive to add this "Pre-request Script" to every request that needs the timestamp. It would be helpful if Postman supported this kind of script at the collection (folder) level. Maybe even better would be for it to support JavaScript when setting values for environment and global variables.

Fortunately, it seems that once the variable has been set, it will remain available for other requests that follow it. They can use the previously-set value without updating it, but it will be getting further in the past.

@srinivas258
Copy link

Hi Could you please let me know how to get delayed timestamp? per1 hour or 2 hour pirior to the sys date?
my request is like below

url?starttime=2017-10-25T12:00&endtime=2017-10-25T13:00 also this is fine(end time = sysdate) rite now i am doing manually want to do it dynamically

http://host.php?timestamp=2017-10-24T13:00&endTimestamp={{timestampUtcIso8601}}

@nikolaas1961
Copy link

Works great thanks for sharing..

@JoeSphere
Copy link

Thanks for this. Real helpful.

@elliz
Copy link

elliz commented May 10, 2022

Note, you can use the native {{$isoTimestamp}} now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment