我是个新手,我尝试验证一个表单。为了进行验证,我想要比较两个日期。所以我得到了今天的日期,并在今天的日期上加上了七天。
const date = new Date();
date.setDate(date.getDate() + 7);
然后,我尝试将此日期与输入字段日期进行比较。
if (values.expiryDate<=date) {
errors.expiryDate = "Expiry date should not be longer than a week.";
}
我试着比较两个这样的日期。但是这段代码不能验证这两天。我该如何解决这个问题?
发布于 2021-08-06 16:28:31
如果values.expiryDate是字符串,则首先将其转换为Date对象。
const d1 = new Date(values.expiryDate);
这样你就可以很容易地比较d1和date了。
if (d1<=date) {
errors.expiryDate = "Expiry date should not be longer than a week.";
}
https://stackoverflow.com/questions/68684714
复制相似问题