使用Javascript获取特定日期的时代

146

如何使用Javascript将07/26/2010转换为UNIX时间戳?

7个回答

255
你可以创建一个Date对象,并在其上调用getTime
new Date(2010, 6, 26).getTime() / 1000

15
日期构造函数的月份参数缺乏一定的一致性,实际上是从零开始计数的。这意味着7代表八月,因此您需要减去1 :-) - Andy E
7
@Andy- 把月份减去1而保留其他部分是一个奇怪的想法,如果JavaScript被用在太空飞船上,这种事情恰恰会让它发疯 :) - Anurag
10
最好使用:new Date("2010-7-26").getTime() / 1000,这样您就不必考虑时区偏移量。 - Vincent McNabb
8
"new Date("2010-7-26")" 在Internet Explorer中无法解析。当传递一个字符串时,Date构造函数依赖于*Date.parse()*方法,该方法在ECMA-262第3版中具有实现相关性,并因此在IE中极不灵活。 - Andy E
可以通过指定时区来最小化可能出现的错误或误解。至少目前(Linux)版本的Firefox、Opera和Chrome支持实例化JavaScript日期,例如 new Date("Jan 1, 1970 GMT+00:00, 00:01"); 这也减少了关于正确月份格式的混淆。 - Michael Besteck

65
new Date("2016-3-17").valueOf() 

将返回一个长的时间戳


10

有些答案没有解释JavaScript日期对象中时区变化的副作用,如果这是你关心的问题,那么你应该考虑这个答案。

方法1:机器时区相关

默认情况下,JavaScript返回一个考虑到机器时区的Date对象,因此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

如果您希望考虑到时区,那么使用自1970年1月1日起的时间来计算距离现在的时间并不是一个问题。因此,如果您想获取基于计算机时区的当前日期或指定日期的自1970年1月1日以来的时间,可以继续使用这种方法。

// 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)

方法二:机器时区无关

但是,如果你想要消除时区的变化,并且获得指定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)

在我看来,除非你知道你在做什么(参见上面的注释),否则应该选择方法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


5

4

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

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()


3

您也可以使用 Date.now() 函数。


1
从技术上讲,没有回答 OP 的问题,即将一个“特定”的日期转换为自纪元以来的秒数。但我喜欢它的简洁性。显然,在大多数情况下,Date.now()new Date().getTime()更好。 - MarkHu

0
Number(new Date(2010, 6, 26))

与上面的内容相同。如果您需要秒数,请不要忘记/ 1000


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接