I have below code in postman's pre-request script --which gives me current date. I rather want yesterday's date (current_timestamp - 1 day).
var current_timestamp = new Date();
postman.setEnvironmentVariable("current_timestamp", current_timestamp.toISOString());
I searched doc & net but could not get the answer. Can someone please help me with reference to date functions --to get my desired result.
Thanks
You can use the momentjs module in Postman to get a date in any format you need. This snippet will bring in the module and set the dates you require in the environment file. Both solutions would give you the same outcome but I prefer to use moment as it's a built-in module that handles dates and times very well.
How can I put current date in Postman? The Best Answer is js with Postman to give you that timestamp format. You can add this to the pre-request script: const moment = require('moment'); pm.
You can use the momentjs module in Postman to get a date in any format you need.
In the Pre-Request Script
, add this to get what you need without using native JS:
var moment = require('moment')
pm.environment.set("current_timestamp", moment().toISOString())
pm.environment.set("current_timestamp - 1 day", moment().subtract(1, 'day').toISOString())
This snippet will bring in the module and set the dates you require in the environment file.
For a non moment
solution in plain JavaScript to just quickly go back 24hrs, you could do something like this:
var yesterday = (Date.now() - 86400000) // 24hrs in ms
pm.environment.set('yesterday', new Date(yesterday).toISOString())
Both solutions would give you the same outcome but I prefer to use moment as it's a built-in module that handles dates and times very well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With