比较2个时间戳来检查30天总是失败



我有以下两个时间戳,我正在尝试检查testTimestamp是否超过30天。。。

// Monday, 9 March 2020 08:59:29
var testTimestamp = 1583744369;
// 30 Days Previous To Current Timestamp
var thirtyDaysTimestamp = new Date().getTime() - (30 * 24 * 60 * 60 * 1000);
if(parseInt(testTimestamp) > parseInt(thirtyDaysTimestamp)) {
console.log('Timestamp is more than 30 days');
} else {
console.log('Timestamp is not more than 30 days');
}

这看起来应该有效,但无论我测试什么时间戳,它总是告诉我是not超过30天。

我哪里错了?

Javascript时间戳以毫秒为单位。因此,2020年3月9日星期一08:59:29是158374369000。并且";较老的";是指";较少";。

此外,如果对值进行硬编码,则可能需要注意时区。

// Monday, 9 March 2020 08:59:29
var testTimestamp = 1583744369000;
// 30 Days Previous To Current Timestamp
var thirtyDaysTimestamp = new Date().getTime() - (30 * 24 * 60 * 60 * 1000);
if(testTimestamp < thirtyDaysTimestamp) {
console.log('Timestamp is more than 30 days');
} else {
console.log('Timestamp is not more than 30 days');
}

最新更新