๐Ÿ• Node.js and Timezones

ยท

1 min read

๐Ÿ• Node.js and Timezones

Have you ever wondered how to deal with time zone differences in your Node.js app? well, say no more!

You can set your desired timezone process.env.TZ to your desired country, which will keep your node application Date objects in sync with your timezone.

process.env.TZ = 'UTC'
console.log(new Date().toLocaleTimeString())
// 11:08:45 AM

process.env.TZ = 'America/Sao_Paulo'
console.log(new Date().toLocaleTimeString())
// 8:08:45 AM

process.env.TZ = 'US/Hawaii'
console.log(new Date().toLocaleTimeString())
// 1:08:45 AM

A list of countries and their timezones can be found here.

Why should we even care about time zones?

Well, that's a great question. Dealing with timezones comes in handy when dealing with requests from (and to) various resources which are heavily dependent on usage and syncing with timezones (Like mobile Push Notifications).

The above trick can be really helpful in such cases since by default the engine takes the default TZ which is set on your OS.

ย