I discovered Heroku a few weeks ago. It's a great platform to deploy your apps, specially because of their Free plan which I've been using as dev env to work with my team remotely.
Lately I started working on a new project. On this particular project I rely on dates for most of the main operations. The app has a front-end built with Angular 5, and a back-end built with Node.js
Everything was working perfectly on my local environment, but when I deployed to Heroku all the date operations started to behave kind of random (spoilers: it wasn't random at all).
Well, here's the thing. I'm from Argentina, and I had the front-end deployed on a local server, but the back-end was on Heroku and the default region for new heroku apps is "us". So basically my front-end and back-end were on different time zones.
This is not the first time I deal with this kind of date issues (there aren't many local cloud services in my region) so I found the source of the problem almost immediately, and I wanted to share it with you in case you encounter a similar issue.
Luckily Heroku let's us change the timezone of our app simply by adding a new config var.
From the Heroku CLI
- Open the command shell
- Login to heroku
- Navigate to your app's directory
- Setup your TZ
heroku config:add TZ="America/Argentina/Buenos_Aires"
From the Heroku dashboard (web)
- Login into the Heroku dashboard from a browser
- Navigate to the app
- Click over the Settings tab
- Press the "Reveal Config Vars" button
- Set the key to "TZ" and the value to your timezone (E.g.: America/Argentina/Buenos_Aires)
- Press the Add button
How do I know which is the correct TZ value
Follow this link to see the complete list of time zones
Check the new TZ value
Use the following command to check the value of the new config var from Heroku CLI:
heroku config:get TZ
Top comments (35)
thanx bro you save my life
Hell yes! You saved me a heart attack. I couldn't figure out why my app that just went live was being screwy with the dates despite my testing and live environments being connected to the same database. Thank you!
Timezone issues are a nightmare! Glad I could help :)
Paula, you are a hero, I didn't realize that would be so easy, I was about to change my.net core api to get my Timezone(Brazil) and let be honest, it would be a headache, and it wasn't necessary, Thank you!!
Glad I could help :D and thanks for reaching out!
Thank you so much
If I just add TZ config and I am doing something which requires system time will it use timezone set by using the TZ ?
Yes, that's how it usually works. I'm not sure if there are any specific cases where that's not the case.
For example, if you deploy a node.js app that does something like
var now = new Date()
, it will use the timezone you specified in your TZ config.Thank you very much! 👍
Time is a complex thing, and Paula you helped me out today. I never imagined it to be soo easy!
I'm glad, Monish!
¡Muchas gracias! Me ayudaste en mi proyecto de tesina.
Me alegro!
Thank you so much for this article! I'm building an app with a small team and we have been fighting with getting proper dates to display. It's consistently been one day behind. This fixed it!
Glad I could help! :)
Thank you men, It worked
Many thanks.
Happy to help!
Many thanks for this advice!
It is REALLY helpful.
Good Luck!
You're welcome!
This just ended my 3-day heartache. Literally. Thank you for sharing.
I'm glad! Thanks for letting me know :)
Very useful thanks
Saved my little ar**!! Can't thank you enough Buddy!