How do I format {{$timestamp}} as MM/DD/YYYY in Postman?
Asked Answered
M

5

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?

Marj answered 17/11, 2017 at 16:27 Comment(6)
you can write JS code in pre-request script tab to do soSisal
@chakri Can you explain in more detail with an answer?Marj
Add below code in pre-request script tab postman.setEnvironmentVariable("$$timestamp",new Date()); and in your request just use {{$$timestamp}}Sisal
---------- #64894460Burlie
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.Calva
@Calva It was a third party API (Kronos Workforce Central) that I had no control over.Marj
D
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/

Danseuse answered 14/12, 2017 at 23:32 Comment(6)
this can't be true.. postman doesn't have support for loading external libraries right now: github.com/postmanlabs/postman-app-support/issues/1180Concubinage
Hooo very nice. This will be helping a los with pre and post scripts. ThanksArrington
To prevent name-conflicts, you might want to use a different name like {{today}} or {{datestamp}}.Marj
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. :)Danseuse
Pretty useful referenced blog postJayme
@Concubinage It's somewhat true: learning.postman.com/docs/writing-scripts/script-references/…Latt
B
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}}"
Belier answered 30/1, 2019 at 8:59 Comment(5)
There's gotta be a way to get a date, without modifying a pre-request script for every request. This is nuts.Chivalrous
There are global variables that you can set once and read other places.Dyne
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 collectionOtherness
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.Craze
Postman now supports {{$isoTimestamp}}, output: "2020-09-16T18:11:41.397Z"Footloose
C
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

Craze answered 18/1, 2020 at 7:18 Comment(2)
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?Danseuse
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.Rhinelandpalatinate
A
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);
Americano answered 19/5, 2020 at 6:38 Comment(0)
G
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

Gallon answered 10/12, 2020 at 10:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.