How to set the default timezone in Node.js [3 Ways]

avatar
Borislav Hadzhiev

Last updated: Apr 5, 2024
4 min

banner

# Table of Contents

  1. How to set the default timezone in Node.js
  2. Using the cross-env package to set the TZ environment variable in Node.js
  3. You can also pass a timezone to the tolocaleString() method

# How to set the default timezone in Node.js

Use the TZ to set the default timezone in Node.js.

The TZ environment variable can be used to specify the timezone configuration.

index.js
process.env.TZ = 'America/New_York'; const date = new Date(); // ๐Ÿ‘‡๏ธ 2023-05-17T10:14:24.106Z console.log(date); // ๐Ÿ‘‡๏ธ 6:14:24 AM console.log(date.toLocaleTimeString()); // ๐Ÿ‘‡๏ธ 5/17/2023 console.log(date.toLocaleDateString()); // ๐Ÿ‘‡๏ธ 5/17/2023, 6:14:24 AM console.log(date.toLocaleString()); // ๐Ÿ‘‡๏ธ Wed May 17 2023 06:14:24 GMT-0400 (Eastern Daylight Time) console.log(date.toString());

set default timezone in node js

The code for this article is available on GitHub

I used America/New_York for the timezone, however, you can pick any other value from the following list.

Make sure to pick a value from the second column (TZ identifier) of the table.

Some commonly used timezones include:

  • Etc/UTC
  • America/Los_Angeles
  • Canada/Central
  • Europe/Berlin
  • Europe/Paris

Note: if you need to set the timezone to UTC, use the Etc/UTC string.

index.js
process.env.TZ = 'Etc/UTC';

Make sure to set the TZ environment variable before calling and Date methods, at the top of your Node.js file.

index.js
process.env.TZ = 'America/New_York'; // access Date methods here

The TZ environment variable is used to specify the timezone configuration.

Node.js supports basic timezone IDs (such as Europe/Paris and America/New_York).

You can use the following methods to print the Date in different formats:

  • toLocaleTimeString
index.js
process.env.TZ = 'America/New_York'; const date = new Date(); // ๐Ÿ‘‡๏ธ 6:14:24 AM console.log(date.toLocaleTimeString());

The toLocaleTimeString method converts a time to a string by using the current locale.

  • toLocaleDateString
index.js
process.env.TZ = 'America/New_York'; const date = new Date(); // ๐Ÿ‘‡๏ธ 5/17/2023 console.log(date.toLocaleDateString());

The toLocaleDateString method converts a date to a string by using the current locale.

  • toLocaleString
index.js
process.env.TZ = 'America/New_York'; const date = new Date(); // ๐Ÿ‘‡๏ธ 5/17/2023, 6:14:24 AM console.log(date.toLocaleString());

The toLocaleString method converts a date and time to a string by using the current locale.

  • toString()
index.js
process.env.TZ = 'America/New_York'; const date = new Date(); // ๐Ÿ‘‡๏ธ Wed May 17 2023 06:14:24 GMT-0400 (Eastern Daylight Time) console.log(date.toString());

The toString() method returns the string representation of the date where the format depends on the current locale.

If you need to view a list of all available timezones, view the second column (TZ identifier) of this Wikipedia table.

# Using the cross-env package to set the TZ environment variable in Node.js

You can also use the cross-env package to set the TZ environment variable.

Open your terminal in your project's root directory (where your package.json file is).

If you don't have a package.json file, run the following command.

shell
npm init -y

Install the cross-env package.

shell
# with NPM npm install cross-env # or with YARN yarn add cross-env

The package enables us to set an environment variable in a way that works on all operating systems (Windows, macOS and Linux).

Update the script that starts your development server in your package.json file.

package.json
{ "scripts": { "dev": "cross-env TZ=America/New_York node index.js" }, "dependencies": {}, "devDependencies": {} }

The dev script above assumes that your entry file is called index.js.

Make sure to replace America/New_York with your preferred timezone.

Your index.js file could be as simple as follows.

index.js
const date = new Date(); // ๐Ÿ‘‡๏ธ 2023-05-17T10:14:24.106Z console.log(date); // ๐Ÿ‘‡๏ธ 6:14:24 AM console.log(date.toLocaleTimeString()); // ๐Ÿ‘‡๏ธ 5/17/2023 console.log(date.toLocaleDateString()); // ๐Ÿ‘‡๏ธ 5/17/2023, 6:14:24 AM console.log(date.toLocaleString()); // ๐Ÿ‘‡๏ธ Wed May 17 2023 06:14:24 GMT-0400 (Eastern Daylight Time) console.log(date.toString());
The code for this article is available on GitHub

Now when you issue the npm run dev command, the TZ environment variable will be set to the specified value.

shell
npm run dev

set timezone environment variable using cross env

You can also set the TZ environment variable directly, but this differs depending on your operating system.

For example, on macOS and Linux, you could use the following approach.

shell
# macOS and Linux export TZ='America/New_York' && node index.js

set timezone environment variable on macos linux

The command above assumes that your entry file is called index.js and your preferred timezone is America/New_York.

If you are on Windows and use CMD, use the following commands instead.

shell
# Windows (CMD - Command Prompt) set TZ=America/New_York node index.js

set node js timezone on windows in cmd

If you are on Windows and use PowerShell, use the following commands instead.

shell
$env:TZ="America/New_York" node index.js

set timezone in node js on windows in powershell

# You can also pass a timezone to the tolocaleString() method

You can also pass a timezone to the toLocaleString() method if you need to initialize a date to a particular timezone.

index.js
const date = new Date(); // ๐Ÿ‘‡๏ธ 5/17/2023, 6:55:31 AM console.log(date.toLocaleString('en-US', 'America/New_York'));
The code for this article is available on GitHub

I have written a detailed guide on how to initialize a JavaScript Date to a particular time zone.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev