Node datetime and timezones

I’m having trouble understanding the current state of affairs with timezones on a node server.

In PHP data is returned like this, which matches what I see in my database tools. These datetimes were auto-generated by mariadb on record creation using current_timestamp(). The database server and mariadb are set to UTC. When I create the records, the datetime displayed is in fact UTC.

Screen Shot 2021-07-17 at 5.19.36 PM

Everything above is what I’m accustomed to and seems correct.

Now in Node I see the following for the same records:

Screen Shot 2021-07-17 at 5.19.47 PM

@patrick points out in other threads, that node datetime values will comes back in UTC, which is what I’m seeing here, except for the fact, that while it is in UTC format, it has also been converted. But the original time was already in UTC, so it seems to have added 7 hours to UTC???

My understanding of mariadb is from the docs:

If a column uses the DATETIME data type, then any inserted values are stored as-is, so no automatic time zone conversions are performed.

So I don’t think maria is getting in the way here. I’m also assuming the node server defaults to UTC, correct?

My goal throughout my app’s is to store and calculate all datetime values based on UTC and only change to local time when displaying to the end user.

I really thought I had a good handle on datetimes, but clearly more to learn!

What the heck am I missing here?

Before anybody spends too much time on this to help me…I THINK, I have figured it out. I believe that the local development node server is not in fact defaulting to UTC, but rather is picking up the timezone of my dev machine. So an environment variable should fix this up.

Confirmed. The local node server defaults to the machine tz, so getting it aligned on UTC to match all my other environments, fixed it right up.

I ran into the same thing a few weeks ago.
How do you plan to set the timezone? I looked at a bunch of options with and without docker, and nothing really worked.

Dotenv seems the most appropriate @sid as it’s handled by default by Wappler. That is how I do it.

But isn’t dotenv thimg supposed to work only in Development?
I have never used it before… Just read this somewhere in community.

Nope. Dotenv will work in any environment. Maybe you have read that in remote cloud environments you should use the native solution used by the cloud provider. That is normally the case for PaaS.

But for your own servers you can use dotenv.

Thanks for the confirmation. :slightly_smiling_face:
Will search how to use dotenv.

1 Like

Hi @JonL . Finally have a use case for Environment Variables in my NodeJS project.
But cannot figure out setting up the variables for different environments - dev, prod & staging.

I found this post from you, talking about dotenv works fine.


But, in the screenshot there, there is a condition specifically to not use dotenv in production. I saw another post from Patrick as well informing that dotenv should not be used in production.

So a bit confused here - how to setup variables for different environements so that they show up correctly in $_ENV object? Please help.

Hi Sid,

Indeed you are right. Wappler will only load dotenv for non-production environments which is the most common scenario as most cloud environments will already have a way for you to set environment variables and load them on the server.

But if you really really need dotenv in production you can preload it.

Of course remember to not commit your .env files to your repository.

https://medium.com/ulangi-app/why-dotenv-is-unnecessary-4ca981c1253

1 Like

Make more sense now. Thanks for the link. :slightly_smiling_face: