Created
September 5, 2018 15:16
-
-
Save jamiepollock/690e624cfc4aca3496375269cb029314 to your computer and use it in GitHub Desktop.
Postman JS Pre-request script for dynamic dates.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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