How to use date and timestamp in postman

Many times we need to pass timestamp or date as request parameter. One way is to use predefined variable like {{$timestamp}}. But this can not be used when it’s needed in specific format. So to achieve that we should use ‘Pre-Request Script’ option. Use below code in pre request script. Here we are using moment class to get specific formats.

var moment = require('moment');
pm.environment.set('timestamp', moment().format(("YYYY-MM-DD-HH.mm.SS.SSSSSS")));
pm.environment.set('date', moment().format(("YYYY-MM-DD")));

Then use these environment variables as parameter like {{timestamp}} or {{date}}

Another need could be to access the time in milliseconds. Add below code in ‘Pre-Request Script’ tab and in it can be accesses as variable.

var date = new Date();
var time_milliseconds = date.getTime();
pm.environment.set("time_milliseconds",time_milliseconds);

Leave a Reply

Your email address will not be published. Required fields are marked *