我可以将node.js日期始终设置为UTC / GMT吗? [英] Can I make node.js Date always be in UTC/GMT?

查看:416
本文介绍了我可以将node.js日期始终设置为UTC / GMT吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序在Linux服务器上运行,时间(自然地)设置为UTC / GMT。但是,该应用是在Mac台式机上开发的,该台式机通常将时间设置为本地时区。

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.

我可以更改每个 new Date()在我的代码中运行:

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.

我了解用于始终返回UTC / GMT的节点的先前版本。有什么办法可以恢复这种行为?

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

编辑:删除了为每个注释在getTime()中添加时区偏移的方法-因为getTime()已经存在于UTC中。

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

推荐答案

您可以按以下方式使用node.js的 TZ 配置参数。

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

用于bash(及相关)

For bash (and related)

export TZ=UTC

对于Powershell

For Powershell

$env:TC = 'UTC'

然后选择两者:

nodejs server/index.js

这篇关于我可以将node.js日期始终设置为UTC / GMT吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆