如何验证邮递员范围内的时间戳?

how to validate timestamp within range in postman?

我有 setTimeout(function(){}, [8000]);在预请求脚本中

GET 响应,时间戳在 10 秒内变化

{"deviceId": null,
"deviceName": null,
"timestamp": "2021-04-08T22:28:26Z"
}

我写的测试用例

var current_timestamp = new Date();
current_timestamp.setSeconds(current_timestamp.getSeconds() - 9);

var current_timestamp1 = new Date();
current_timestamp1.setSeconds(current_timestamp1.getSeconds() - 8);


var current_timestamp3 = new Date();
current_timestamp3.setSeconds(current_timestamp3.getSeconds() - 15);

pm.test("Retrieve currentTimeStamp ", function(){    pm.expect(jsonData.items[0].timestamp).to.be.oneOf([current_timestamp.toISOString().split('.')[0] + 'Z',current_timestamp1.toISOString().split('.')[0] + 'Z']);

)];

以上代码有效,但我想要
内的东西 10 秒范围,因为响应时间更长,我无法继续 在数组中添加项目 所以我尝试了

var jsonData = pm.response.json();
var responseTime = jsonData.items[0].timestamp;
tests["Response time is acceptable"] = _.inRange(responseTime,       current_timestamp1.toISOString().split('.')[0], current_timestamp3.toISOString().split('.')[0]);

这不work.Can有人帮忙吗?

let moment = require('moment');

pm.test("Check the timestamp range", function () {
    pm.expect(moment( "2021-04-08T22:28:26Z").isBetween("2021-02-08T22:28:26Z",  "2021-04-08T22:28:27Z")).to.be.true;
});

您可以在脚本沙箱中使用 moment 库。