0

i wanted to make the default timezone as Asia/calcutta in node js

i have tried process.env.TZ='Asia/calcutta'

Even after changing the timezone to Asia/calcutta when i try to print today's date, i'm still getting UTC timezone.

new Date()

2021-12-04T09:54:48.152Z

process.env.TZ='Asia/calcutta'

'Asia/calcutta'

new Date()

2021-12-04T09:55:33.447Z

Sangavi
  • 1
  • 2

1 Answers1

0

Here is a simple way to get custom timezone in node.js

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

console.log(nDate);

Note: This method has a critical problem IMO. Node versions < 13 only have locale data for 'en-us', which means it throws (in some cases) or gives the wrong date format for every other country. And in many cases, the date will look correct but day & month are swapped! A good way is to just use a moment.js and add/subtract the time required

Satindar31
  • 195
  • 15