How to read timestamp and how to subtract two timestamps?

允我心安 提交于 2020-12-12 02:11:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!