如何比较邮递员中的两个日期
How to compare two dates in Postman
我有一个场景,我需要将 API 响应中的日期与今天的日期进行比较,并检查它是否小于或等于。
API 响应将给我一个 JSON 对象,其中 JSON 键“License”将包含格式为“13-Aug-2019”的日期,我需要将此日期与今天的日期进行比较,如果今天的日期大于“13-Aug-2019”,则测试结果失败。
下面是我为获取许可证字符串中的日期和今天的日期而编写的代码,
Var body = JSON.parse(response Body);
Var body date=body["license"]
//Sample license value is " license cs code1 version 13-aug-
2018 cnts ......"
var words = bodydate.split(' ');
license_valid_till=words[4]; // This will get the string 13-aug-
2019
console.log(license_valid_till)
var ts = new Date();
var part=ts.toDateString();
var dpart = part.split(' ');
today_date=dpart[2] + "-" +dpart[1] +"-"+ dpart[3];
//This will get the string 12-aug-2019
console.log(today_date)
现在我的问题是如何比较这两个值,有什么建议会有很大帮助吗?
你可以使用momentjs。
var moment = require('moment')
使用momentjs的例子:https://jsfiddle.net/onigetoc/rzyz4wgp/
如果你想在没有任何第三方库的情况下做到这一点,那么你可以使用下面的代码片段。
pass=true;
fail=false;
var responseDate = "13-Aug-2019";
var currentDate;
var months = ['Jan', 'Feb', 'Mar','Apr','May','Jun','Jul','Aug','Sept','Oct','Nov','Dec'];
if(responseCode.code === 204){
var today = new Date();
var dd = today.getDate();
var MMM = months[today.getMonth()];
var yyyy = today.getFullYear();
today = dd+'-'+MMM+'-'+yyyy;
console.log("Current Time for event is -> "+today);
if(responseDate === today){
console.log("Pass");
tests["ResponseDate and current date does matched "+responseDate] = responseDate === today;
}else{
console.log("Fail");
tests["ResponseDate and current date does not matched "+responseDate] = responseDate !== today;
}
}else{
console.log("Request failed ...!");
}
您可以将 responseDate 值替换为您的运行时响应日期值。
您可以使用 if 条件来检查大于、等于或小于操作。
您可以使用 Postman echo API 来执行此操作 - 请查看给定的 link 并查看是否这就是您要查找的内容。
https://docs.postman-echo.com/?version=latest#b709b99c-3347-40fc-2c21-98ceb7f9e267
我们有相同的需求,所以,我们将分享我们的完整解决方案。
它 运行 使用邮递员的 Test 选项,在这种情况下,它确实使用来自 get 的数据并使用 Moment.js[ 处理它=26=],基本上:
- 使用 Get 检索数据
- 检查 JSON 响应是否正常
- 加载moment.js
- 将日期值增加 20 天
- 比较一下日期比今天大
- 创建包含所选记录的数组
- Post到Restfull数据库(可以用http://restdb.io测试Get和Post)
测试代码示例
var moment = require('moment');
pm.test("response must be valid and have a body", function () {
pm.response.to.be.ok;
pm.response.to.be.withBody;
pm.response.to.be.json;
});
var slots = pm.response.json();
var myPost = [];
var todo = [
{
"_id": "5d13e83bb1c3633400002841"
}
];
var hoje = moment();
for (let thisItem of slots) {
var changedOk = moment(thisItem._changed).add(20, 'days');
if (moment(changedOk).isBefore(hoje))
myPost.push( {
_id: thisItem._id,
toDo : todo,
approved: true,
onHalt: true,
p: false
});
}
console.log(myPost);
pm.sendRequest({
url: 'https://mydbase-0000.restdb.io/rest/slots',
method: 'POST',
header: { 'cache-control': 'no-cache',
'content-type': 'application/json',
'x-apikey': 'xxxxxxxxxxxxxxxxxxxxxx'},
body: {
mode: 'raw',
raw: JSON.stringify(myPost)
}
}, function (err, res) {
console.log(res);
});
我有一个类似的场景:我需要比较邮递员中的两个日期,日期格式如“2022-01-16T19:40:51.816”。
所以我用这个:
// (dates from JSON for my case)
// var date1 = "2022-01-16T19:40:51.816";
// var date2 = "2022-01-16T17:40:51.816";
// (today for your case)
// var today = new Date();
pm.expect(Date.parse(date1)).to.be.above(Date.parse(date2));
我有一个场景,我需要将 API 响应中的日期与今天的日期进行比较,并检查它是否小于或等于。
API 响应将给我一个 JSON 对象,其中 JSON 键“License”将包含格式为“13-Aug-2019”的日期,我需要将此日期与今天的日期进行比较,如果今天的日期大于“13-Aug-2019”,则测试结果失败。
下面是我为获取许可证字符串中的日期和今天的日期而编写的代码,
Var body = JSON.parse(response Body);
Var body date=body["license"]
//Sample license value is " license cs code1 version 13-aug-
2018 cnts ......"
var words = bodydate.split(' ');
license_valid_till=words[4]; // This will get the string 13-aug-
2019
console.log(license_valid_till)
var ts = new Date();
var part=ts.toDateString();
var dpart = part.split(' ');
today_date=dpart[2] + "-" +dpart[1] +"-"+ dpart[3];
//This will get the string 12-aug-2019
console.log(today_date)
现在我的问题是如何比较这两个值,有什么建议会有很大帮助吗?
你可以使用momentjs。
var moment = require('moment')
使用momentjs的例子:https://jsfiddle.net/onigetoc/rzyz4wgp/
如果你想在没有任何第三方库的情况下做到这一点,那么你可以使用下面的代码片段。
pass=true;
fail=false;
var responseDate = "13-Aug-2019";
var currentDate;
var months = ['Jan', 'Feb', 'Mar','Apr','May','Jun','Jul','Aug','Sept','Oct','Nov','Dec'];
if(responseCode.code === 204){
var today = new Date();
var dd = today.getDate();
var MMM = months[today.getMonth()];
var yyyy = today.getFullYear();
today = dd+'-'+MMM+'-'+yyyy;
console.log("Current Time for event is -> "+today);
if(responseDate === today){
console.log("Pass");
tests["ResponseDate and current date does matched "+responseDate] = responseDate === today;
}else{
console.log("Fail");
tests["ResponseDate and current date does not matched "+responseDate] = responseDate !== today;
}
}else{
console.log("Request failed ...!");
}
您可以将 responseDate 值替换为您的运行时响应日期值。 您可以使用 if 条件来检查大于、等于或小于操作。
您可以使用 Postman echo API 来执行此操作 - 请查看给定的 link 并查看是否这就是您要查找的内容。
https://docs.postman-echo.com/?version=latest#b709b99c-3347-40fc-2c21-98ceb7f9e267
我们有相同的需求,所以,我们将分享我们的完整解决方案。 它 运行 使用邮递员的 Test 选项,在这种情况下,它确实使用来自 get 的数据并使用 Moment.js[ 处理它=26=],基本上:
- 使用 Get 检索数据
- 检查 JSON 响应是否正常
- 加载moment.js
- 将日期值增加 20 天
- 比较一下日期比今天大
- 创建包含所选记录的数组
- Post到Restfull数据库(可以用http://restdb.io测试Get和Post)
测试代码示例
var moment = require('moment');
pm.test("response must be valid and have a body", function () {
pm.response.to.be.ok;
pm.response.to.be.withBody;
pm.response.to.be.json;
});
var slots = pm.response.json();
var myPost = [];
var todo = [
{
"_id": "5d13e83bb1c3633400002841"
}
];
var hoje = moment();
for (let thisItem of slots) {
var changedOk = moment(thisItem._changed).add(20, 'days');
if (moment(changedOk).isBefore(hoje))
myPost.push( {
_id: thisItem._id,
toDo : todo,
approved: true,
onHalt: true,
p: false
});
}
console.log(myPost);
pm.sendRequest({
url: 'https://mydbase-0000.restdb.io/rest/slots',
method: 'POST',
header: { 'cache-control': 'no-cache',
'content-type': 'application/json',
'x-apikey': 'xxxxxxxxxxxxxxxxxxxxxx'},
body: {
mode: 'raw',
raw: JSON.stringify(myPost)
}
}, function (err, res) {
console.log(res);
});
我有一个类似的场景:我需要比较邮递员中的两个日期,日期格式如“2022-01-16T19:40:51.816”。
所以我用这个:
// (dates from JSON for my case)
// var date1 = "2022-01-16T19:40:51.816";
// var date2 = "2022-01-16T17:40:51.816";
// (today for your case)
// var today = new Date();
pm.expect(Date.parse(date1)).to.be.above(Date.parse(date2));