最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - get the date - 3 months - Stack Overflow

matteradmin7PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far