Faisal
Faisal

Reputation: 441

Nodejs and mysql database timezone issue

am having a bad time with the timezone of my server

am using nodejs as backend and express routes for services

i changed the time for the server with the correct one using:-

dpkg-reconfigure tzdata

and i checked the server time and seems good

am using

data

to get the current time and it's correct for my country (Saudi Arabia)

the problem is when the record created in the database still give me the wrong time within a delay of 3 hours !!!!

so the database time and server is correct, how come the record created with wrong time ?

Upvotes: 1

Views: 1141

Answers (2)

Kevin Crosby
Kevin Crosby

Reputation: 29

I remember this fixed a bug regarding time zones.

var timeZoneDifference=(this.date.getTimezoneOffset()-dateFuture.getTimezoneOffset())/60;

Upvotes: 0

gaganshera
gaganshera

Reputation: 2639

The time that's being saved in the db is in UTC. Since the Saudi Arabia time is UTC + 3h, you see a 3 hours gap in the time. I advise you to keep the db timezone in UTC as it is, and whenever you retrieve a record, add the 3 hours to the time to convert it to Saudi time.
Similarly, if you want to search by time, subtract 3 ours from your time that you want to search, thereby converting it to UTC time.

Upvotes: 2

Related Questions