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


Question

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

1
122
9/2/2016 4:22:23 PM

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)'

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

105
3/24/2012 5:55:54 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon