Javascript Epoch Time In Days

自作多情 提交于 2020-01-12 07:38:06

问题


I need the epoch time in days. I've seen posts on how to translate it to date but none in days. I'm pretty bad with epoch time...how could I get this?


回答1:


I need the epoch time in days

I'll interpret that you want the number of days since the epoch. The epoch itself is day zero (or the start of day 1, however you want to view it).

At the heart of a javascript Date object is a number of milliseconds since 1970-01-01T00:00:00Z. So to get the number of days from then to now you simply get the current time value and divide it by the number of milliseconds in one day:

var now = new Date();
var fullDaysSinceEpoch = Math.floor(now/8.64e7);

For 2012-10-05 you should get 15618. Not sure if it allows for leap seconds and such, but it should be close enough (within a few seconds) if the system clock is accurate.

It is only when reading values of a Date object (such as getHours() and toString()) that the timezone offset is applied to give local times.



来源:https://stackoverflow.com/questions/12739171/javascript-epoch-time-in-days

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