如何在 node.js 中设置默认时区? [英] How can I set the default timezone in node.js?

查看:68
本文介绍了如何在 node.js 中设置默认时区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 node.js 中设置默认时区?

How do I set the default timezone in node.js?

推荐答案

根据 这个google group thread,你可以在调用任何日期函数之前设置TZ环境变量.刚刚测试过,效果很好.

According to this google group thread, you can set the TZ environment variable before calling any date functions. Just tested it and it works.

> process.env.TZ = 'Europe/Amsterdam' 
'Europe/Amsterdam'
> d = new Date()
Sat, 24 Mar 2012 05:50:39 GMT
> d.toLocaleTimeString()
'06:50:39'
> ""+d
'Sat Mar 24 2012 06:50:39 GMT+0100 (CET)'

不过,您以后不能更改时区,因为那时 Node 已经读取了环境变量.

You can't change the timezone later though, since by then Node has already read the environment variable.

这篇关于如何在 node.js 中设置默认时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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