138

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?

Stevoisiak
  • 23,794
  • 27
  • 122
  • 225

5 Answers5

198

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/

Danny Dainton
  • 23,069
  • 6
  • 67
  • 80
  • this can't be true.. postman doesn't have support for loading external libraries right now: https://github.com/postmanlabs/postman-app-support/issues/1180 – abbood May 31 '18 at 11:45
  • 1
    Hooo very nice. This will be helping a los with pre and post scripts. Thanks – Pablo Palacios Jun 25 '18 at 00:47
  • 6
    To prevent name-conflicts, you might want to use a different name like `{{today}}` or `{{datestamp}}`. – Stevoisiak Sep 06 '18 at 20:11
  • Sure, it was purely an example of how you could use it. The variable name obviously needs to suit your own needs / context or what you're doing, is just copy and pasting my example into your solution...which is never a good thing. :) – Danny Dainton Sep 06 '18 at 22:16
  • 1
    Pretty useful referenced blog post – artu-hnrq Jan 25 '21 at 03:47
  • 1
    @abbood It's somewhat true: https://learning.postman.com/docs/writing-scripts/script-references/postman-sandbox-api-reference/#using-external-libraries – ruslaniv Oct 03 '22 at 09:32
71

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}}"
n-verbitsky
  • 552
  • 2
  • 9
  • 20
Payam Khaninejad
  • 7,692
  • 6
  • 45
  • 55
  • 6
    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 '19 at 22:03
  • 3
    There are global variables that you can set once and read other places. – Morgan Kenyon May 02 '19 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 '19 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. – DeadlyChambers Sep 09 '20 at 20:17
  • 8
    Postman now supports {{$isoTimestamp}}, output: "2020-09-16T18:11:41.397Z" – Lance Sep 16 '20 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

DeadlyChambers
  • 5,217
  • 4
  • 44
  • 61
  • 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? – Danny Dainton Sep 23 '20 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](https://stackoverflow.com/questions/47355150/#comment102391582_54436721) that suggests setting a var with code for many tests at once. – ruffin Oct 05 '21 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);
Arulkumar
  • 12,966
  • 14
  • 47
  • 68
Dheeraj
  • 332
  • 3
  • 4
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": "SANKARB@GMAIL.COM",
"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

Sankarasai
  • 73
  • 5