Last updated: Apr 5, 2024
Reading timeยท4 min
Use the TZ
to set the default timezone in Node.js.
The TZ
environment variable can be used to specify the timezone
configuration.
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());
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.
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.
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
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
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
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()
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.
cross-env
package to set the TZ
environment variable in Node.jsYou 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.
npm init -y
Install the cross-env package.
# 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.
{ "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.
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());
Now when you issue the npm run dev
command, the TZ
environment variable will
be set to the specified value.
npm run dev
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.
# macOS and Linux export TZ='America/New_York' && node index.js
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.
# Windows (CMD - Command Prompt) set TZ=America/New_York node index.js
If you are on Windows and use PowerShell, use the following commands instead.
$env:TZ="America/New_York" node index.js
tolocaleString()
methodYou can also pass a timezone to the toLocaleString()
method if you need to
initialize a date to a particular timezone.
const date = new Date(); // ๐๏ธ 5/17/2023, 6:55:31 AM console.log(date.toLocaleString('en-US', 'America/New_York'));
I have written a detailed guide on how to initialize a JavaScript Date to a particular time zone.
You can learn more about the related topics by checking out the following tutorials: