如何检查时间戳是否超过 3 小时?
我有以下格式的时间戳:
15.03.2011 18:42
下面的函数怎么写?
function isMoreThan3HoursOld(timestamp) {
// parse the timestamp
// compare with now (yes, client side time is ok)
// return true if the timestamp is more than 3 hours old
}
(我需要经常这样做,即在一个具有大约 500 个时间戳的表上(50 行,10 列带有时间戳))
问候
拉尔西
最佳答案
应该解释一下:
var nowMinus3h = new Date,
dateToTest = new Date('2011/03/15 18:42');
nowMinus3h.setMinutes(nowMinus3h.getMinutes()-180); //=> now minus 180 minutes
// now the difference between the dateToTest and 3 hours ago can be calculated:
var diffMinutes = Math.round( (dateToTest - nowMinus3h )/1000/60) ;
如果您需要将“15.03.2011 18:42”转换为日期对象的有效输入,请使用:
var dat_ = '15.03.2011 18:42'.split(' '),
dat = dat_[0].split('.').reverse().join('/')+' '+dat_[1];
关于javascript - 检查现在是否距前 3 小时以内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5855990/