问题
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);
回答1:
You can get the difference between the two dates in milliseconds if you do:
var diffInMillis = c_date.getTime() - final_time.getTime()
To find out if this is less than 1 hour you can do this:
var isLessThan1Hour = diffInMillis < 60 * 60 * 1000;
回答2:
Instead of directly subtracting the timestamps try to diff the dates from which you can individually get hours.From then on one can do a simple maths to do the difference.
That's a more clear and maintainable approach
来源:https://stackoverflow.com/questions/19903490/how-to-read-timestamp-and-how-to-subtract-two-timestamps