How do I set the default timezone in Node.js? I've been able to find 0 to no documentation on this.
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.
Unfortunately, setting process.env.TZ
doesn't work really well (see here for details, basically it's indeterminate when the change will be effective).
So setting the system's timezone before starting node is your only proper option.
However, if you can't do that, it should be possible to use node-time as a workaround: get your times in local or UTC time, and convert them to the desired timezone. See this answer for details.