使用 Javascript 获取特定日期的纪元
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3367415/
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
Get epoch for a specific date using Javascript
提问by Asim Zaidi
How do I convert 07/26/2010to a UNIX timestamp using Javascript?
如何07/26/2010使用 Javascript转换为 UNIX 时间戳?
回答by Michael Mrozek
回答by Jyo Banerjee
new Date("2016-3-17").valueOf()
will return a long epoch
将返回一个很长的纪元
回答by Mitch Dempsey
Take a look at http://www.w3schools.com/jsref/jsref_obj_date.asp
看看http://www.w3schools.com/jsref/jsref_obj_date.asp
There is a function UTC()that returns the milliseconds from the unix epoch.
有一个函数UTC()可以返回 unix 纪元的毫秒数。
回答by Neha M
You can also use Date.now() function.
您还可以使用 Date.now() 函数。
回答by Andrii Los
Number(new Date(2010, 6, 26))
Works the same way as things above. If you need seconds don't forget to / 1000
工作方式与上述相同。如果你需要几秒钟不要忘记/ 1000
回答by Diego Queiroz
Some answers does not explain the side effects of variations in the timezone for JavaScript Date object. So you should consider this answer if this is a concern for you.
一些答案没有解释 JavaScript Date 对象时区变化的副作用。因此,如果这对您来说是个问题,您应该考虑这个答案。
Method 1: Machine's timezone dependent
方法 1:取决于机器的时区
By default, JavaScript returns a Date considering the machine's timezone, so getTime()result varies from computer to computer. You can check this behavior running:
默认情况下,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
This is not a problem if you really want the time since Epoch considering your timezone. So if you want to get time since Epoch for the current Date or even a specified Date based on the computer's timezone, you're free to continue using this method.
如果考虑到您的时区,您真的想要自 Epoch 以来的时间,这不是问题。因此,如果您想获取自 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)
Method 2: Machine's timezone independent
方法 2:机器的时区无关
However, if you want to get ride of variations in timezone and get time since Epoch for a specified Date in UTC(that is, timezone independent), you need to use Date.UTCmethod or shift the date from your timezone to UTC:
但是,如果您想摆脱时区的变化并获取自 Epoch 以来指定日期的 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
So, using this method (or, alternatively, subtracting the difference), the result should be:
因此,使用此方法(或者,减去差异),结果应该是:
// 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, unless you know what you're doing (see note above), you should prefer Method 2, since it is machine independent.
IMO,除非您知道自己在做什么(请参阅上面的注释),否则您应该更喜欢Method 2,因为它与机器无关。
End note
尾注
Although the recomendations in this answer, and since Date.UTCdoes not work without a specified date/time, you may be inclined in using the alternative approach and doing something like this:
尽管此答案中的建议,并且由于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" (?)
This does not make any sense and it is probably WRONG(you are modifying the date). Be aware of not doing this. If you want to get time since Epoch from the current date AND TIME, you are most probably OK using Method 1.
这没有任何意义,而且可能是错误的(您正在修改日期)。请注意不要这样做。如果您想从当前日期AND TIME获取自 Epoch 以来的时间,您很可能可以使用方法 1。
回答by Sourabh
Date.parse()method parses a string representation of a date, and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC.
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
Explore more at: Date.parse()
探索更多:Date.parse()

