Steps to SetUp Project

Step 1: Create a new directory for your project and initialize it with npm:

mkdir myapp
cd myapp
npm init -y

Step 2: Install the necessary packages/libraries in your project using the following commands.

npm install express

Project structure:

The updated dependencies in package.json file will look like:

"dependencies": {
"express": "^4.19.2",
}

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: Implementation to show how set default timezone in Node.js for Windows.

Node
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");
});

Step to Run Application: Run the application using the following command from the root directory of the project

node app.js

Output:

Example 2: Implementation to show how set default timezone in Node.js for Windows with another example.

Node
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: Implementation to show how set default timezone in Node.js for Windows.

Node
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");
});

Step to Run Application: Run the application using the following command from the root directory of the project

node app.js

Output:

How Set Default Timezone in Node.js for Windows ?

Node.js is mainly used for non-blocking, event-driven servers because of its single-threaded nature. It is utilized for traditional websites and back-end API services but was created with real-time, push-based architectures in mind. By default, Node.js uses the system’s timezone for date and time operations. However, there may be situations where you need to set a specific timezone for your Node.js application regardless of the system’s timezone settings. This article will walk you through the process of setting the default timezone in Node.js for Windows. Additionally, we will cover how to install Node.js.

Similar Reads

Steps to SetUp Project

Step 1: Create a new directory for your project and initialize it with npm:...

Conclusion

Setting the default timezone in a Node.js application on Windows can be achieved using the moment-timezone and timezone-support libraries. By following the steps outlined in this article, you can ensure that your Node.js application consistently operates in the desired timezone, regardless of the system’s timezone settings. This is particularly useful for applications that need to manage date and time operations across different regions and timezones....

Contact Us