我可以让节点。js日期总是在UTC/GMT?

时间:2022-05-09 08:35:24

My app runs on Linux servers, where the time (naturally) set to UTC/GMT. However the app is developed on Mac desktops where the time is typically set to a local timezone.

我的应用程序运行在Linux服务器上,时间(自然)设置为UTC/GMT。然而,这款应用程序是在Mac台式机上开发的,在那里时间通常被设置为一个本地时区。

I could change every new Date() in my code to run:

我可以在代码中更改每一个新的日期()来运行:

var date = new Date().getTime();

And thus ensure dates on the server are always GMT, but that seems inelegant.

因此,确保服务器上的日期总是GMT,但这似乎不太优雅。

I understand previous versions of node used to always return UTC/GMT. Is there any way to bring this behavior back?

我理解以前的节点版本总是返回UTC/GMT。有没有办法让这种行为恢复?

Edit: Removed adding timezone offset to getTime() per comments - since getTime() is already in UTC.

编辑:删除了在每个注释中向getTime()添加时区偏移量——因为getTime()已经在UTC中。

3 个解决方案

#1


13  

You can use TZ configuration parameter of node.js as follows

可以使用节点的TZ配置参数。js如下

export TZ=utc 
nodejs server/index.js

#2


9  

From the MDN docs on Date#getTime:

从MDN文档的日期#getTime:

The value returned by the getTime method is the number of milliseconds since 1 January 1970 00:00:00 UTC.

getTime方法返回的值是自1970年1月1日00:00:00 UTC以来的毫秒数。

Assuming you're storing dates/times as numbers (which I would recommend), getTime is already UTC, always.

假设您将日期/时间存储为数字(我建议这样做),getTime始终是UTC。

Suppose your client then requests a date from the server. If the server provides the date as timestamp, which is a number, the client can then do:

假设您的客户端然后从服务器请求一个日期。如果服务器以时间戳的形式提供日期,即数字,则客户端可以:

new Date(timestamp);

And it will be correctly adjusted to the local time on the client.

并根据客户的本地时间进行正确调整。

Of course, maybe I'm misunderstanding your problem. But I just want to point out that this...

当然,也许我误解了你的问题。但是我想指出的是…

new Date().getTime() + new Date().getTimezoneOffset();

...should never really make sense. It's taking a UTC-based time and then offsetting it further, in essence double-offsetting a time.

…应该永远不会有意义。它需要一个基于utc的时间,然后进一步抵消,本质上是双倍补偿时间。

#3


3  

moment.js is a popular node and browser module for dealing with dates. It has a utc mode that should give you what you need. http://momentjs.com/docs/#/manipulating/utc/

的时刻。js是处理日期的流行节点和浏览器模块。它有一个utc模式,可以满足您的需要。http://momentjs.com/docs/ /操作/ utc /

#1


13  

You can use TZ configuration parameter of node.js as follows

可以使用节点的TZ配置参数。js如下

export TZ=utc 
nodejs server/index.js

#2


9  

From the MDN docs on Date#getTime:

从MDN文档的日期#getTime:

The value returned by the getTime method is the number of milliseconds since 1 January 1970 00:00:00 UTC.

getTime方法返回的值是自1970年1月1日00:00:00 UTC以来的毫秒数。

Assuming you're storing dates/times as numbers (which I would recommend), getTime is already UTC, always.

假设您将日期/时间存储为数字(我建议这样做),getTime始终是UTC。

Suppose your client then requests a date from the server. If the server provides the date as timestamp, which is a number, the client can then do:

假设您的客户端然后从服务器请求一个日期。如果服务器以时间戳的形式提供日期,即数字,则客户端可以:

new Date(timestamp);

And it will be correctly adjusted to the local time on the client.

并根据客户的本地时间进行正确调整。

Of course, maybe I'm misunderstanding your problem. But I just want to point out that this...

当然,也许我误解了你的问题。但是我想指出的是…

new Date().getTime() + new Date().getTimezoneOffset();

...should never really make sense. It's taking a UTC-based time and then offsetting it further, in essence double-offsetting a time.

…应该永远不会有意义。它需要一个基于utc的时间,然后进一步抵消,本质上是双倍补偿时间。

#3


3  

moment.js is a popular node and browser module for dealing with dates. It has a utc mode that should give you what you need. http://momentjs.com/docs/#/manipulating/utc/

的时刻。js是处理日期的流行节点和浏览器模块。它有一个utc模式,可以满足您的需要。http://momentjs.com/docs/ /操作/ utc /