使用Javascript获取特定日期的纪元


Answers:


181

您可以创建一个Date对象,然后调用getTime它:

new Date(2010, 6, 26).getTime() / 1000

12
日期构造函数的month参数缺乏一致性,实际上是从零开始的。这意味着7是8月,因此您需要减去1 :-)
Andy E

6
@Andy-这是一个很奇怪的想法,可以在保留其余时间的同时将-1个月抵消,而且如果在其中使用JavaScript,那正是可以使航天飞机发疯的事情:)
Anurag

6
可能更好:new Date(“ 2010-7-26”)。getTime()/ 1000,因此您不必考虑偏移量。
Vincent McNabb

5
@Vincent:new Date("2010-7-26")将不会在Internet Explorer中解析。 传递字符串时,日期构造函数依赖于Date.parse()方法,这是ECMA-262第三版中依赖于实现的结果,因此,众所周知,它在IE中不灵活。
安迪E

也可以将指定时区的可能错误或误解降到最低。至少实际(Linux-)版本的Firefox,Opera和Chrome支持将JavaScript日期实例化为新的Date(“ 1970年1月1日,格林尼治标准时间+00:00,00:01”);这也减少了对正确的月份格式的困惑。
迈克尔Besteck





0

一些答案不能解释JavaScript Date对象时区变化的副作用。因此,如果您对此感到担心,则应考虑此答案。

方法1:取决于机器的时区

默认情况下,JavaScript会根据机器的时区返回日期,因此getTime()结果因计算机而异。您可以检查此行为是否在运行:

new Date(1970, 0, 1, 0, 0, 0, 0).getTime()
    // Since 1970-01-01 is Epoch, you may expect ZERO
    // but in fact the result varies based on computer's timezone

如果您确实希望从Epoch开始考虑您的时区,那么这不是问题。因此,如果您想获取自当前时间到当前日期甚至是基于计算机时区的指定日期的时间,则可以继续使用此方法。

// Seconds since Epoch (Unix timestamp format)

new Date().getTime() / 1000             // local Date/Time since Epoch in seconds
new Date(2020, 11, 1).getTime() / 1000  // time since Epoch to 2020-12-01 00:00 (local timezone) in seconds

// Milliseconds since Epoch (used by some systems, eg. JavaScript itself)

new Date().getTime()                    // local Date/Time since Epoch in milliseconds
new Date(2020,  0, 2).getTime()         // time since Epoch to 2020-01-02 00:00 (local timezone) in milliseconds

// **Warning**: notice that MONTHS in JavaScript Dates starts in zero (0 = January, 11 = December)

方法2:与机器的时区无关

但是,如果您想驾驭时区的变化并获取自大纪元以来UTC中指定日期的时间(即与时区无关),则需要使用Date.UTC方法或将日期从您的时区更改为UTC:

Date.UTC(1970,  0, 1)
    // should be ZERO in any computer, since it is ZERO the difference from Epoch

    // Alternatively (if, for some reason, you do not want Date.UTC)
    const timezone_diff = new Date(1970, 0, 1).getTime()  // difference in milliseconds between your timezone and UTC
    (new Date(1970,  0, 1).getTime() - timezone_diff)
    // should be ZERO in any computer, since it is ZERO the difference from Epoch

因此,使用此方法(或者,减去差),结果应为:

// Seconds since Epoch (Unix timestamp format)

Date.UTC(2020,  0, 1) / 1000  // time since Epoch to 2020-01-01 00:00 UTC in seconds

    // Alternatively (if, for some reason, you do not want Date.UTC)
    const timezone_diff = new Date(1970, 0, 1).getTime()
    (new Date(2020,  0, 1).getTime() - timezone_diff) / 1000  // time since Epoch to 2020-01-01 00:00 UTC in seconds
    (new Date(2020, 11, 1).getTime() - timezone_diff) / 1000  // time since Epoch to 2020-12-01 00:00 UTC in seconds

// Milliseconds since Epoch (used by some systems, eg. JavaScript itself)

Date.UTC(2020,  0, 2)   // time since Epoch to 2020-01-02 00:00 UTC in milliseconds

    // Alternatively (if, for some reason, you do not want Date.UTC)
    const timezone_diff = new Date(1970, 0, 1).getTime()
    (new Date(2020,  0, 2).getTime() - timezone_diff)         // time since Epoch to 2020-01-02 00:00 UTC in milliseconds

// **Warning**: notice that MONTHS in JavaScript Dates starts in zero (0 = January, 11 = December)

IMO,除非您知道自己在做什么(请参见上面的注释),否则应该首选方法2,因为它与机器无关。


尾注

尽管此答案中的建议,并且由于Date.UTC没有指定的日期/时间也无法使用,所以您可能倾向于使用替代方法并执行以下操作:

const timezone_diff = new Date(1970, 0, 1).getTime()
(new Date().getTime() - timezone_diff)  // <-- !!! new Date() without arguments
    // means "local Date/Time subtracted by timezone since Epoch" (?)

这没有任何意义,可能是错误的(您正在修改日期)。请注意不要这样做。如果您想从当前日期时间开始到大纪元,那么使用方法1可能就可以了。


0

Date.parse()方法解析日期的字符串表示形式,并返回自以来的毫秒数January 1, 1970, 00:00:00 UTC

const unixTimeZero = Date.parse('01 Jan 1970 00:00:00 GMT');
const javaScriptRelease = Date.parse('04 Dec 1995 00:12:00 GMT');

console.log(unixTimeZero);
// expected output: 0

console.log(javaScriptRelease);
// expected output: 818035920000

有关更多信息,请访问:Date.parse()

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.