问题描述
我需要几天的纪元时间。我看过有关如何将其翻译成日期的帖子,但几天都没有。我的时间非常糟糕...我怎么能得到这个?
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'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).
javascript日期对象的核心是毫秒自1970-01-01T00:00:00Z。因此,要获得从那时到现在的天数,您只需获取当前时间值并将其除以一天内的毫秒数:
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);
对于2012-10-05,你应该得到15618.不确定它是否允许闰秒等,但如果系统时钟准确,它应该足够接近(在几秒钟内)。
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.
只有在读取Date对象的值时(例如 getHours()
和)时区偏移量用于给出当地时间。
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.
这篇关于Javascript大纪元时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!