gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 04:29:02 25 4
gpt4 key购买 nike

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

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

最佳答案

JavaScript 时间戳始终基于 UTC :

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

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

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

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

关于javascript - node.js 时区独立 Date.now(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18450851/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com