How do I set the default timezone in node.js?
To reset the default time zone to local, use moment. tz. setDefault with no arguments.
you can also use moment(). format('Z') to get the same result. This gives you the current offset in your current timezone but this may vary for part of the year due to DST changes. If in doubt, always use/store UTC date/times plus the timezone identifier and then convert to local as late as possible.
Use the toUTCString() method to get the current date and time in utc, e.g. new Date(). toUTCString() .
Sometimes, we may want to initialize a JavaScript date to a particular time zone. The default time zone is UTC.
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.
Another approach which seemed to work for me at least in Linux environment is to run your Node.js application like this:
env TZ='Europe/Amsterdam' node server.js
This should at least ensure that the timezone is correctly set already from the beginning.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With