Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Previous day in Postman request

Tags:

postman

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

like image 588
Jack Avatar asked Jan 25 '18 11:01

Jack


People also ask

How do I find past Postman dates?

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 do you set the Postman's date today?

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.


1 Answers

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.

like image 186
Danny Dainton Avatar answered Oct 23 '22 13:10

Danny Dainton