Skip to content

Instantly share code, notes, and snippets.

@jamiepollock
Created September 5, 2018 15:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamiepollock/690e624cfc4aca3496375269cb029314 to your computer and use it in GitHub Desktop.
Save jamiepollock/690e624cfc4aca3496375269cb029314 to your computer and use it in GitHub Desktop.
Postman JS Pre-request script for dynamic dates.
var moment = require('moment');
// Allows dates to be offset in the past rather than right now. offset format "hh:mm:ss".
// "24:00:00" would be 24hrs in the past.
var offsetValue = pm.globals.get("offset");
var offset = moment.duration(offsetValue);
// Determines the duration between start & end dates format "hh:mm:ss"
// "00:30:00" would be a 30 minute delay.
var durationValue = pm.globals.get("duration");
var duration = moment.duration(durationValue);
// Calculate
var startTime = moment().subtract(offset).subtract(duration);
var endTime = moment().subtract(offset);
// set for reuse in the request, these could be globals or environment variables. They're always recalculated on a new request.
pm.environment.set('startTime', startTime.format());
pm.environment.set('endTime', endTime.format());
// With my current understanding, quotes are required otherwise date is not outputted as a string literal
{
"StartDate": "{{startDate}}",
"EndDate": "{{endDate}}".
//.. Some other data
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment