简体   繁体   English

得到日期 - 3个月

[英]get the date - 3 months

I'm trying to get the current date - 3 months and use it in a postman Pre-request script. 我正在尝试获取当前日期 - 3个月并在邮递员预先请求脚本中使用它。 I'm told it uses javascript, but it doesn't seem to be working. 我被告知它使用javascript,但它似乎没有工作。

The error I get is: 我得到的错误是:

There was an error in evaluating the Pre-request Script: TypeError: startDate.setMonth is not a function 评估预请求脚本时出错:TypeError:startDate.setMonth不是函数

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 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; JavaScript Date对象只能通过调用JavaScript Date作为构造函数来实例化:将其作为常规函数(即没有new运算符)调用将返回一个字符串而不是Date对象; unlike other JavaScript object types, JavaScript Date objects have no literal syntax. 与其他JavaScript对象类型不同,JavaScript Date对象没有文字语法。

so 所以

Date();

needs to be 需要是

new Date();

Try change var startDate = Date(); 尝试更改var startDate = Date(); to var startDate = new Date(); to var startDate = new Date();

As an alternative, Postman comes with the moment module built in so you could do something like this: 作为替代方案,Postman附带了内置的时刻模块,因此您可以执行以下操作:

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. 或者您显然可以使用本机JavaScript,但值得了解几种不同的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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