NodeJS 检查时间是否已到期

vktxenjb  于 2023-03-08  发布在  Node.js
关注(0)|答案(2)|浏览(182)

嗨,伙计们,支票日期是不是过期了?
我用是透明JS
我想它只能检查几天,我需要所有的
我希望检查是正确的,但它就像检查一天,而不是一个小时或什么。

var date = new Date();
let add_day = date.setDate(date.getDate() + 1);
let minus_day = date.setDate(date.getDate() - 2); // x
let fors = (...args) => (new Date(args[0]));

console.log(add_day + ' | add days | ' + fors(add_day).toLocaleString());
console.log(minus_day + ' | minus day | ' + fors(minus_day).toLocaleString());

if (add_day > minus_day) { 
  console.log('date has expired')
} else {
  console.log('date has not expired')
}
juud5qan

juud5qan1#

您正在更新(date)变量两次,最终导致两天相同。尝试创建一个如下所示的新Date

var date1 = new Date();
var date2 = new Date();
let add_day = date1.setDate(date1.getDate() + 1);
let minus_day = date2.setDate(date2.getDate() - 0 );
let fors = (...args) => (new Date(args[0]));

console.log(new Date(minus_day))
console.log(new Date(add_day))

console.log(add_day + ' | add days | ' + fors(add_day).toLocaleString());
console.log(minus_day + ' | minus day | ' + fors(minus_day).toLocaleString());

if (add_day > minus_day) { 
  console.log('date has expired')
} else {
  console.log('date has not expired')
}
cqoc49vn

cqoc49vn2#

输出:

您可以看到,我添加了**+30秒**到日期,它工作正常

var date = new Date();
let add_day  = new Date(date);

// add_day.setDate(date.getDate() + 1); // Add one day to today's date
add_day.setSeconds(add_day.getSeconds() + 30); // Add 30 seconds to the expiration date

let minus_day = date.setDate(date.getDate())
let fors = (...args) => (new Date(args[0]));

console.log(' add days | ' + fors(add_day).toLocaleString());
console.log(' minus day | ' + fors(minus_day).toLocaleString());

if (add_day > minus_day) { 
  console.log('date has expired')
} else {
  console.log('date has not expired')
}

相关问题