javascript node.js 时区独立 Date.now()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18450851/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 11:59:06  来源:igfitidea点击:

node.js timezone independent Date.now()

javascriptnode.js

提问by ArkahnX

What is a common way to sync timeStamps across servers and clients in node.js, not dependent on timezone?

在 node.js 中跨服务器和客户端同步时间戳的常用方法是什么,而不依赖于时区?

e.g., a Date.now() equivalent that would provide the same time on the server and client. Preferably without any node.js modules, or client side libraries.

例如,在服务器和客户端上提供相同时间的 Date.now() 等价物。最好没有任何 node.js 模块或客户端库。

回答by Jonathan Lonowski

JavaScript timestamps are always based in UTC:

JavaScript 时间戳始终基于UTC

Time is measured in ECMAScript in milliseconds since 01 January, 1970 UTC.

时间在 ECMAScript 中以 UTC 时间 1970 年 1 月 1 日以来的毫秒为单位进行测量。

Date strings from different timezones can have the same timestamp.

来自不同时区的日期字符串可以具有相同的时间戳。

var a = "2013-08-26 12:00 GMT-0800";
var b = "2013-08-27 00:00 GMT+0400";

console.log(Date.parse(a) === Date.parse(b)); // true
console.log(Date.parse(a)); // 1377547200000
console.log(Date.parse(b)); // 1377547200000

And, Date.now()should return relatively similar values across systems.

并且,Date.now()应该返回跨系统的相对相似的值。