以天为单位的 Javascript 纪元时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12739171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript Epoch Time In Days
提问by dman
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?
我需要以天为单位的纪元时间。我已经看过有关如何翻译它的帖子,但几天后就没有了。我对纪元时间很糟糕......我怎么能得到这个?
回答by RobG
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).
我会解释你想要自纪元以来的天数。纪元本身是第 0 天(或第 1 天的开始,但是您想查看它)。
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:
javascript Date 对象的核心是自 1970-01-01T00:00:00Z 以来的毫秒数。因此,要获得从那时到现在的天数,您只需获取当前时间值并将其除以一天中的毫秒数:
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.
对于 2012-10-05,你应该得到 15618。不确定它是否允许闰秒等,但如果系统时钟准确,它应该足够接近(在几秒钟内)。
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.
只有在读取 Date 对象(例如getHours()
和toString()
)的值时,才会应用时区偏移量以提供本地时间。