How do I format {{$timestamp}} as MM/DD/YYYY in Postman?
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/
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}}"
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"
Then set Pre-Request Scripts for the all calls, so the global variable is always available.
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);