简体   繁体   中英

get the date - 3 months

I'm trying to get the current date - 3 months and use it in a postman Pre-request script. I'm told it uses javascript, but it doesn't seem to be working.

The error I get is:

There was an error in evaluating the Pre-request Script: TypeError: startDate.setMonth is not a function

Here is what I have:

// setup start date
var startDate =  Date();
startDate.setMonth(startDate.getMonth() - 3);

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#Syntax

JavaScript Date objects can only be instantiated by calling JavaScript Date as a constructor: calling it as a regular function (ie without the new operator) will return a string rather than a Date object; unlike other JavaScript object types, JavaScript Date objects have no literal syntax.

so

Date();

needs to be

new Date();

Try change var startDate = Date(); to var startDate = new Date();

As an alternative, Postman comes with the moment module built in so you could do something like this:

var moment = require("moment")
var startTime = moment().subtract(3, 'months')

Or you could obviously use native JavaScript, worth knowing a couple of different ways though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM