Node.js在哪里获得它的时区以及如何在全局设置它? [英] Where does Node.js get it's timezone and how can I set it globally?

查看:745
本文介绍了Node.js在哪里获得它的时区以及如何在全局设置它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行Windows 10 Professional,我的时区和区域设置设置为布里斯班/澳大利亚(UTC + 10:00)。此外,我正在我的系统上为我正在构建的应用程序运行Node.js.

I am running Windows 10 Professional and my timezone and region settings are set to Brisbane/Australia (UTC+ 10:00). Furthermore, I am running Node.js on my system for an application I am building.

我在Node.js中运行了以下内容:

I ran the following in Node.js:

  var x = new Date();
  console.log(x);

它返回以下内容:


2017-09-07T23:42:33.719Z

2017-09-07T23:42:33.719Z

注意日期时间字符串末尾的Z?
这代表祖鲁时间。 (UTC + 0)

Notice the Z at the end of the datetime string? This represents Zulu time. (UTC + 0)

我认为当没有指定时区时,默认情况下会在Node.js中设置此项。如何在Node.js中全局指定时区,以确保所有日期对象都正确返回?

I presume that this is set by default in Node.js when no timezone is specified. How can I specify the timezone globally in Node.js so as to ensure that all date objects a returned correctly?

推荐答案

你可以将TZ env设置为时区字符串。

You can set the TZ env to a timezone string.

例如:

$ export TZ=Europe/Amsterdam
$ node

> Date()
'Fri Sep 08 2017 03:02:57 GMT+0200 (CEST)'


$ export TZ=America/Anchorage
$ node

> Date()
'Thu Sep 07 2017 17:04:46 GMT-0800 (AKDT)'

您还可以在运行时设置 process.env.TZ

You can also set process.env.TZ at runtime:

> process.env.TZ = 'Antarctica/Mawson'

> Date()
'Thu Sep 07 2017 17:11:00 GMT-0800 (+05)'

注意,无论时区如何, new Date()返回UTC 2017-09-08T01:05:58.103Z 这样调用时。

Note, regardless of the timezone, new Date() returns UTC 2017-09-08T01:05:58.103Z when called like this.

这篇关于Node.js在哪里获得它的时区以及如何在全局设置它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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