Javascript 检查现在是否少于3小时前

Javascript 检查现在是否少于3小时前,javascript,datetime,Javascript,Datetime,如何检查时间戳是否超过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 } (我需要经常这样做,那是在

如何检查时间戳是否超过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列有时间戳))

问候


Larsi

将时间戳更改为毫秒,并以毫秒为单位计算时间差。

要检查此差异,不需要天、月和年。只有几小时几分钟

如果你把它读成一个字符串,或者可能是两个字符串,你可以这样做

function check()
{
int minutes1;
int minutes2;
minutes1=hour_loaded*60 + minutes_loaded;
minutes2=hour_now*60 + minutes_now;
if( abs(minutes1-minutes2)> 180 )
return false;
}
还要做一些陈述来检查小时数是:22还是23。因为如果加载的小时数为22,而现在的小时数为1,则此方法不起作用。

应该解释一下:

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];

newdate()
但在其他方面,这是一个很好的答案+1嗨,davin,日期也可以用
新日期
实例化,真的,请看。我知道它是有效的,尽管我认为它不是标准的。刚刚检查了ECMAScript 5规范,它看起来有效…没错。见生产新表达式:新的新表达式,EcMasPEC 262 ED 5,PAR。11.2.2 ;)嗯,首先你说你不需要几天,然后你说如果一天过去了,这种方法就行不通了。