DEV Community

Raphaël Badia
Raphaël Badia

Posted on

How to avoid and debug most of timezone problems in production

Have you ever been struggling to reproduce a weird bug happening only on production?
Some offset between what you have in local (that obviously works perfectly) and what your clients see on this evil, inaccessible server?

If you're using anything other than Windows Server, there are chances your server is using the UTC timezone, which is not - unless you live in England - the same timezone than your computer.

The simplest solution to reproduce what's happening on your server is to set your timezone to UTC. And there is a way to run node in UTC without changing your computer timezone!

To use UTC as your node timezone, you can simply set it in the env before running node, like so :

TZ=utc node index.js
Enter fullscreen mode Exit fullscreen mode

It also works with other timezones, for instance this would set the timezone to Amsterdam's one:

TZ='Europe/Amsterdam' node server/index.js
Enter fullscreen mode Exit fullscreen mode

I recommand setting it in your package.json to always develop to stay as close as possible to production's conditions. In my package.json it looks like this :

{
    "scripts": {
        "dev": "TZ=utc nodemon --max_old_space_size=8192 server/index.js",
    }
}
Enter fullscreen mode Exit fullscreen mode

And if you're not using UTC on production, you should definitely read The worst Server Setup Mistake You Can Make.

Good luck debugging your timezone issues !

Top comments (3)

Collapse
 
wallacesf profile image
Wallace Ferreira

Thank you. TypeORM was returning 3 hours ahed from timestamp column fields. This env fixed the problem.

Collapse
 
emilbuszylo profile image
Emil Buszyło

Thank you so much !
You have just rescued my Sunday afternoon !

Collapse
 
shriram1056 profile image
shriram1056 • Edited

Hi Raphaël,

I want the timezone to be based on location to account for timezone shift, how can i do that?