3

I have the following code to get (timestamp) and (NOW timestamp). I subtract them to get the difference between them but I get a number like 123456 and I can not understand how much this number represent. I want to check if the difference between those two dates are less than one hour, how?

final_time = new Date(2013, 11, 11, 11, 11);
c_date = new Date();
offset_time = c_date.getTimezoneOffset();
var n1 = Math.abs(offset_time);
current_date = new Date(c_date.getTime() - n1 * 60 * 1000);

alert(current_date-final_time);
4

2 回答 2

6

如果您这样做,您可以获得两个日期之间的差异(以毫秒为单位):

var diffInMillis = c_date.getTime() - final_time.getTime()

要确定这是否少于 1 小时,您可以执行以下操作:

var isLessThan1Hour = diffInMillis < 60 * 60 * 1000;
于 2013-11-11T10:05:00.773 回答
0

与其直接减去时间戳,不如尝试区分您可以单独获得小时数的日期。从那时起,人们可以做一个简单的数学来计算差异。

这是一种更清晰和可维护的方法

于 2013-11-11T10:04:47.967 回答