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

node.jsTimezone

node.js Problem Overview


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

node.js Solutions


Solution 1 - node.js

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.

Solution 2 - node.js

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.

Solution 3 - node.js

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

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

console.log(nDate);

Solution 4 - node.js

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.

Solution 5 - node.js

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

Solution 6 - node.js

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.

Solution 7 - node.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)'

Solution 8 - node.js

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,

Solution 9 - node.js

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

Solution 10 - node.js

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.

Solution 11 - node.js

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"
  }
}

Solution 12 - node.js

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

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

Solution 13 - node.js

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

Solution 14 - node.js

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

Solution 15 - node.js

i work with this npm package unix-system-timezone

Solution 16 - node.js

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

Solution 17 - node.js

This works; https://stackoverflow.com/a/53282481/17700714

var date = new Date('2016-08-25T00:00:00')
var userTimezoneOffset = date.getTimezoneOffset() * 60000;
new Date(date.getTime() - userTimezoneOffset);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionOluView Question on Stackoverflow
Solution 1 - node.jswebjprgmView Answer on Stackoverflow
Solution 2 - node.jsuhefView Answer on Stackoverflow
Solution 3 - node.jsVikas KandariView Answer on Stackoverflow
Solution 4 - node.jsLaurent CouvidouView Answer on Stackoverflow
Solution 5 - node.jsAlireza MortezaeiView Answer on Stackoverflow
Solution 6 - node.jsMariano RuizView Answer on Stackoverflow
Solution 7 - node.jsNickView Answer on Stackoverflow
Solution 8 - node.jsPrajyot KhandeparkarView Answer on Stackoverflow
Solution 9 - node.jsElBartoView Answer on Stackoverflow
Solution 10 - node.jspsclView Answer on Stackoverflow
Solution 11 - node.jsuser3896501View Answer on Stackoverflow
Solution 12 - node.jsMarshalView Answer on Stackoverflow
Solution 13 - node.jsrainerhahnekampView Answer on Stackoverflow
Solution 14 - node.jsBillyBob - Fire Fight'n PrgrmRView Answer on Stackoverflow
Solution 15 - node.jsjeriveromartinezView Answer on Stackoverflow
Solution 16 - node.jsCristhian DView Answer on Stackoverflow
Solution 17 - node.jsJonasView Answer on Stackoverflow