admin管理员组

文章数量:1025493

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);

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);
Share Improve this question edited Oct 17, 2018 at 1:00 jasonscript 6,1783 gold badges30 silver badges45 bronze badges asked Apr 19, 2018 at 17:59 user117499user117499
Add a ment  | 

3 Answers 3

Reset to default 4

https://developer.mozilla/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 (i.e. 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 es 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.

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);

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);
Share Improve this question edited Oct 17, 2018 at 1:00 jasonscript 6,1783 gold badges30 silver badges45 bronze badges asked Apr 19, 2018 at 17:59 user117499user117499
Add a ment  | 

3 Answers 3

Reset to default 4

https://developer.mozilla/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 (i.e. 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 es 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.

本文标签: javascriptget the date3 monthsStack Overflow