我有一个 curTime
变量,它是使用 new Date()
的当前时间和一个从后端数据调用的 pwChangeDate
值。
let curTime = new Date(); // Thu Oct 27 2022 15:02:34 GMT+0900
const pwDate = new Date(pwChangeDate) // Thu Oct 20 2022 13:51:57 GMT+0900
此时,当 pwDate
基于 curTime
超过 90 天时,我想显示一条提示“90 天已过”。当 83 天过去时,“90 天还剩 7 天”。我想显示一个警报。
但是如果我使用我的代码它不起作用我该如何修复它?
const pwChangeDate = cookie.get('pwChangeDate');
const pwDate = new Date(pwChangeDate)
if (curTime.getDate() >= pwDate.getDate() - 90) {
alert('90 days have passed.')
}
if (curTime.getDate() >= pwDate.getDate() - 7) {
alert('7 days left out of 90 days..')
}
最佳答案
你可以像这样得到当前数据和 pwDate 之间的差异:
const pwDate = new Date('Thu Oct 20 2022 13:51:57 GMT+0900');
const diff = Math.floor((new Date - pwDate)/1000/60/60/24);
console.log(diff)
关于javascript - 如何在 Javascript 中使用新日期执行条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74217524/