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

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

This question is related to node.js timezone

The answer is


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.


Set server timezone and use NTP sync. Here is one better solution to change server time.

To list timezones

timedatectl list-timezones

To set timezone

sudo timedatectl set-timezone America/New_York

Verify time zone

timedatectl

I prefer using UTC timezone for my servers and databases. Any conversions must be handled on client. We can make used of moment.js on client side.

It will be easy to maintain many instances as well,


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.


just set environment variable in your main file like index.js or app.js or main.js or whatever your file name is:

process.env.TZ = "Asia/Tehran";

this will set the timezone which will be used in your entire node application


The solution env TZ='Europe/Amsterdam' node server.js from @uhef works in cases where your app doesn't work with forked process, but when you are working with forked process, specially when you launch your app with a building tool like gulp , the command gulp will take the env values, but the process created by gulp not (your app).

To solve this, you have to do:

$ export TZ="Europe/Amsterdam"; gulp myTask

This will set the TZ environment variable for all the process started in the console you are working on, included all the subsequents process executed after the gulp command in the same console without the need to execute them with the prefix export TZ="Europe/Amsterdam"; again.


Unfortunately, setting process.env.TZ doesn't work really well - 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 How to use timezone offset in Nodejs? for details.


Sometimes you may be running code on a virtual server elsewhere - That can get muddy when running NODEJS or other flavors.

Here is a fix that will allow you to use any timezone easily.

Check here for list of timezones

Just put your time zone phrase within the brackets of your FORMAT line.

In this case - I am converting EPOCH to Eastern.

//RE: https://www.npmjs.com/package/date-and-time
const date = require('date-and-time');

let unixEpochTime = (seconds * 1000);
const dd=new Date(unixEpochTime);
let myFormattedDateTime = date.format(dd, 'YYYY/MM/DD HH:mm:ss A [America/New_York]Z');
let myFormattedDateTime24 = date.format(dd, 'YYYY/MM/DD HH:mm:ss [America/New_York]Z');

I know this thread is very old, but i think this would help anyone that landed here from google like me.

In GAE Flex (NodeJs), you could set the enviroment variable TZ (the one that manages all date timezones in the app) in the app.yaml file, i leave you here an example:

app.yaml

# [START env]
env_variables:
  # Timezone
  TZ: America/Argentina/Buenos_Aires

Here is a 100% working example for getting custom timezone Date Time in NodeJs without using any external modules:

_x000D_
_x000D_
const nDate = new Date().toLocaleString('en-US', {_x000D_
  timeZone: 'Asia/Calcutta'_x000D_
});_x000D_
_x000D_
console.log(nDate);
_x000D_
_x000D_
_x000D_


You can config global timezone with the library tzdata:

npm install tzdata -y

After set your environment with the variable for example: TZ=America/Bogota

And testing in your project:

new Date()

I hope helpyou


Update for node.js v13

As @Tom pointed out, full icu support is built in v13 now. So the setup steps can be omitted. You can still customize how you want to build or use icu in runtime: https://nodejs.org/api/intl.html

For node.js on Windows, you can do the following:

  1. Install full-icu if it has been installed, which applies date locales properly

    npm i full-icu or globally: npm i -g full-icu

  2. Use toLocaleString() in your code, e.g.:

    new Date().toLocaleString('en-AU', { timeZone: 'Australia/Melbourne' })

    This will produce something like: 25/02/2019, 3:19:22 pm. If you prefer 24 hours, 'en-GB' will produce: 25/02/2019, 15:19:22

For node.js as Azure web app, in addition to application settings of WEBSITE_TIME_ZONE, you also need to set NODE_ICU_DATA to e.g. <your project>\node_modules\full-icu, of course after you've done the npm i full-icu. Installing the package globally on Azure is not suggested as that directory is temporary and can be wiped out.

Ref: 1. NodeJS not applying date locales properly

  1. You can also build node.js with intl options, more information here

You can take moment timezone. It lets you set your location and also takes care of daylight saving time.


i work with this npm package unix-system-timezone


You could enforce the Node.js process timezone by setting the environment variable TZ to UTC

So all time will be measured in UTC+00:00

Full list: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones

Example package.json:

{
  "scripts": {
    "start": "TZ='UTC' node index.js"
  }
}

As of Node 13, you can now repeatedly set process.env.TZ and it will be reflected in the timezone of new Date objects. I don't know if I'd use this in production code but it would definitely be useful in unit tests.

> process.env.TZ = 'Europe/London';
'Europe/London'
> (new Date().toString())
'Fri Mar 20 2020 09:39:59 GMT+0000 (Greenwich Mean Time)'

> process.env.TZ = 'Europe/Amsterdam';
'Europe/Amsterdam'
> (new Date().toString())
'Fri Mar 20 2020 10:40:07 GMT+0100 (Central European Standard Time)'

Here's an answer for those deploying a Node.js application to Amazon AWS Elastic Beanstalk. I haven't seen this documented anywhere else:

Under Configuration -> Software -> Environment Properties, simply set the key value pair TZ and your time zone e.g. America/Los Angeles, and Apply the change.

You can verify the effect by outputting new Date().toString() in your Node app and paying attention to the time zone suffix.