DEV Community

Cover image for Time Zone: Nodejs
Abayomi Ogunnusi
Abayomi Ogunnusi

Posted on

Time Zone: Nodejs

I've faced a lot of obstacles while developing web apps, but I've never had to deal with time zone issues.
I decided to write this brief post to help anyone who might find themselves in the same situation.
The solution was discovered thanks to the help of a colleague.

timezone

After writing the logic for a service apartment booking app check site here, I discovered that the email was not sending the correct time in the booking receipt. I live in Africa, Nigeria to be precise yet the time zone supplied to the user after a successful booking was eastern standard time i.e the previous day to my current time. In my case:
Fri Dec 17 2021 to Sat Dec 18 2021 instead of Sat Dec 18 2021 to Sun Dec 19 2021
Image description

✅ The quick fix was:



const express = require('express');
const app = express();
const port = process.env.PORT || 4545

process.env.TZ ="Africa/Lagos"

...

app.listen(port, () => {
console.log(Server Established and running on Port ⚡</span><span class="p">${</span><span class="nx">port</span><span class="p">}</span><span class="s2">)
})

Enter fullscreen mode Exit fullscreen mode




Conclusion

You can easily set your time zone to a correct value by using process.env.TZ ="Continent/Country". I hope this post was helpful. Thanks for reading.

Top comments (5)

Collapse
 
drsimplegraffiti profile image
Abayomi Ogunnusi

@mandraketech noted and thanks for the contribution

Collapse
 
mandraketech profile image
Navneet Karnani

The cleaner fix would have been to set the TZ environment on the machine where you were running this process. That would make it apply systemwide.

Collapse
 
drsimplegraffiti profile image
Abayomi Ogunnusi • Edited

@rawas_aditya glad you found it helpful

Collapse
 
drsimplegraffiti profile image
Abayomi Ogunnusi

@dirkecker You are welcome

Collapse
 
rawas_aditya profile image
Aditya Rawas

whats a timing i was just looking for this one