我有一种情况,我需要将API响应中的日期与今天的日期进行比较,并检查该日期是否小于或等于。
API响应将给我一个JSON对象,并且在该JSON中,密钥“许可证”将包含格式为“ 13-Aug-2019”的日期,我需要将此日期与今天的日期进行比较,如果今天的日期为大于“ 2019年8月13日”表示测试结果失败。
下面是我编写的用于获取许可证字符串中的日期和今天的日期的代码,
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)
现在我的问题是如何比较这两个值,任何建议都会有很大帮助?
答案 0 :(得分:1)
如果要在没有任何第三方库的情况下进行操作,则可以使用以下代码段。
@OneToMany(
mappedBy = "queue_group",fetch = FetchType.LAZY,
cascade = CascadeType.ALL
)
@JsonManagedReference
private Set<Queue> queues;
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name = "qid")
// @JsonIgnore
@JsonBackReference
private Queue_group queue_group;
您可以使用运行时响应日期值替换responseDate值。 您可以使用if条件来检查大于,等于或小于运算。
答案 1 :(得分:1)
我们有着完全相同的需求,因此,我们将分享完整的解决方案。 它使用邮递员的 Test 选项运行,在这种情况下,它确实使用了get中的数据并使用 Moment.js 处理它,基本上是:
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);
});
答案 2 :(得分:0)
答案 3 :(得分:0)
您可以使用Postman echo API来执行此操作-请查看给定的链接,看看您是否正在寻找它。
https://docs.postman-echo.com/?version=latest#b709b99c-3347-40fc-2c21-98ceb7f9e267