150

In Postman, the dynamic variable {{$timestamp}} inserts the current Unix Time Stamp into a request. (Represented as the number of seconds since January 1, 1970)

"currentTime": "1510934784"

However, the API I am working with expects timestamps formatted as MM/DD/YYYY.

"currentDate": "11/17/2017"

How do I insert the current date (formatted as MM/DD/YYYY) into my request with Postman?

6
  • you can write JS code in pre-request script tab to do so Nov 17, 2017 at 16:32
  • @chakri Can you explain in more detail with an answer?
    – Stevoisiak
    Nov 17, 2017 at 16:35
  • 2
    Add below code in pre-request script tab postman.setEnvironmentVariable("$$timestamp",new Date()); and in your request just use {{$$timestamp}} Nov 17, 2017 at 16:46
  • ---------- stackoverflow.com/questions/64893960/…
    – Pap Malik
    Nov 23, 2020 at 12:02
  • Fix your API... that is the worst possible choice for timestamps, since many parts of the world use DD/MM/YYYY and for days from 1-12 there is no way to distinguish between that poor choice and yours. Use YYYY-MM-DD.
    – JoelFan
    Dec 7, 2023 at 19:40

5 Answers 5

210

You could use moment.js with Postman to give you that timestamp format.

You can add this to the pre-request script:

const moment = require('moment');
pm.globals.set("today", moment().format("MM/DD/YYYY"));

Then reference {{today}} where ever you need it.

If you add this to the Collection Level Pre-request Script, it will be run for each request in the Collection. Rather than needing to add it to all the requests individually.

For more information about using moment in Postman, I wrote a short blog post: https://dannydainton.com/2018/05/21/hold-on-wait-a-moment/

6
77

Use Pre-request script tab to write javascript to get and save the date into a variable:

const dateNow = new Date();
pm.environment.set('currentDate', dateNow.toISOString());

and then use it in the request body as follows:

"currentDate": "{{currentDate}}"
5
  • 7
    There's gotta be a way to get a date, without modifying a pre-request script for every request. This is nuts.
    – Triynko
    Apr 30, 2019 at 22:03
  • 3
    There are global variables that you can set once and read other places. May 2, 2019 at 16:16
  • 3
    Put the requests in a folder or collection, then you can set these kind of variables once in the pre-script of the folder or the collection
    – Diceyus
    Sep 18, 2019 at 12:04
  • See my answer above this one. It shows you how to set pre-scripts on a collection, so you don't have to put it in each individual request. Sep 9, 2020 at 20:17
  • 11
    Postman now supports {{$isoTimestamp}}, output: "2020-09-16T18:11:41.397Z"
    – Lance
    Sep 16, 2020 at 18:14
23

My solution is similar to Payam's, except I am using

//older code
//postman.setGlobalVariable("currentDate", new Date().toLocaleDateString());
pm.globals.set("currentDate", new Date().toLocaleDateString());

If you hit the "3 dots" on the folder and click "Edit"

enter image description here

Then set Pre-Request Scripts for the all calls, so the global variable is always available.

enter image description here

2
  • 1
    You could also use - pm.globals.set("currentDate", new Date().toLocaleDateString()); Wouldn't this give you a timestamp in the format "DD/MM/YYY" rather than "MM/DD/YYYY" though? Sep 23, 2020 at 13:31
  • 1
    Useful to note the last picture [currently] includes, "This script will execute before every request in this collection." That is, this answer is an excellent description of Diceyus' improvement to Payam's answer that suggests setting a var with code for many tests at once.
    – ruffin
    Oct 5, 2021 at 13:02
6

Any future date in JavaScript (postman test uses JavaScript) can be retrieved as:

var dateNow = new Date();  
var twoWeeksFutureDate = new Date(dateNow.setDate(dateNow.getDate() + 14)).toISOString();

postman.setEnvironmentVariable("future-date", twoWeeksFutureDate);
3

In PostMan we have ->Pre-request Script. Paste the Below snippet.

const dateNow = new Date();
postman.setGlobalVariable("todayDate", dateNow.toLocaleDateString());

And now we are ready to use.

{
    "firstName": "SANKAR",
    "lastName": "B",
    "email": "[email protected]",
    "creationDate": "{{todayDate}}"
}

If you are using JPA Entity classes then use the below snippet

@JsonFormat(pattern="MM/dd/yyyy")
@Column(name = "creation_date")
private Date creationDate;

enter image description here enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.