javascript - get the date - 3 months - Stack Overflow

I'm trying to get the current date - 3 months and use it in a postman Pre-request script. I'm

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745636424a4637421.html

相关推荐

  • javascript - get the date - 3 months - Stack Overflow

    I'm trying to get the current date - 3 months and use it in a postman Pre-request script. I'm

    2小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信