Aut*_*cus 84 javascript javascript-framework
如何07/26/2010使用Javascript 转换为UNIX时间戳?
Mic*_*zek 158
new Date(2010, 6, 26).getTime() / 1000
Run Code Online (Sandbox Code Playgroud)
一些答案没有解释 JavaScript Date 对象时区变化的副作用。因此,如果这对您来说是个问题,您应该考虑这个答案。
方法 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
Run Code Online (Sandbox Code Playgroud)
如果考虑到您的时区,您真的想要自 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)
Run Code Online (Sandbox Code Playgroud)
方法 2:机器的时区无关
但是,如果您想摆脱时区的变化并获取自 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
Run Code Online (Sandbox Code Playgroud)
因此,使用此方法(或者,减去差异),结果应该是:
// 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)
Run Code Online (Sandbox Code Playgroud)
IMO,除非您知道自己在做什么(请参阅上面的注释),否则您应该更喜欢Method 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" (?)
Run Code Online (Sandbox Code Playgroud)
这没有任何意义,而且可能是错误的(您正在修改日期)。请注意不要这样做。如果您想从当前日期AND TIME获取自 Epoch 以来的时间,您很可能可以使用方法 1。
请查看http://www.w3schools.com/jsref/jsref_obj_date.asp
有一个函数UTC()返回unix时代的毫秒数.
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
Run Code Online (Sandbox Code Playgroud)
探索更多信息:Date.parse()
| 归档时间: |
|
| 查看次数: |
81158 次 |
| 最近记录: |