Skip to content
Related Articles

Related Articles

Improve Article

How set default timezone in Node.js for Windows ?

  • Difficulty Level : Easy
  • Last Updated : 01 Jul, 2021
Geek Week

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 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 default timezone in Nodejs for Windows.

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

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

2. Set the variable using newDate.

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

3. 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:

app.js




var 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 below command:

node app.js

Output:

Example 2:

app.js




var 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 below command:

node app.js

Output:

Example 3:

app.js




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

Run app.js file using below command:

node app.js

Output:




My Personal Notes arrow_drop_up
Recommended Articles
Page :