Open In App

How set default timezone in Node.js for Windows ?

Last Updated : 31 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

NodeJS is primarily used for non-blocking, event-driven servers, due to its single-threaded nature. It’s used for traditional websites and back-end API services but was designed with real-time, push-based architectures in mind.

In this article, we will see how to set the default timezone in Node.js for windows. You can learn how to install Nodejs from here.

Installing module: Install the express module using the following command.

npm install express

Project structure: Our project structure will look like this.

There are many methods we can try to set the default timezone in Nodejs for Windows.

Set in code using process.env.tz sample below –

process.env.TZ = "Asia/Calcutta";
console.log(new Date().toString());

Set the variable using newDate.

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

We can config global timezone with the library tzdata in our code:

npm install tzdata -yN

Now in app.js set the value of TZ.

TZ = 'Asia/Calcutta'
console.log(new Date().toString());

Example 1:

Javascript




const express = require('express'),
app = express();
 
// Method 1
const nDate = new Date().toLocaleString('en-US', {
    timeZone: 'Asia/Calcutta'
});
 
console.log(nDate);
 
app.listen(3000, function () {
    console.log("Express Started on Port 3000");
});


Run app.js file using the below command:

node app.js

Output:

Example 2:

Javascript




const express = require('express'),
app = express();
 
// Method 2
process.env.TZ = "Asia/Calcutta";
console.log(new Date().toString());
 
 
app.listen(3000, function () {
    console.log("Express Started on Port 3000");
});


Run app.js file using the below command:

node app.js

Output:

Example 3:

Javascript




const express = require('express'),
app = express();
 
// Method 3
TZ = 'Asia/Calcutta'
console.log(new Date().toString());
 
app.listen(3000, function () {
    console.log("Express Started on Port 3000");
});


Run the app.js file using the below command:

node app.js

Output:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads