Search code examples
mysqljsondatedatetimestringify

How to prevent JSON.stringify() from altering date?


I read all pages on SO about this, but none has the simple solution for what I need.

In MySQL database, I have DATETIME field called 'start'. An example record in it is:

2015-12-28 06:20:00

When I fetch this from database, date in result object is same as in database, but when I use JSON.stringify method on result object, it saves this date as 2015-12-28 05:20:00.

How can I prevent this, I just want the same date as it is in database, without any conversion to local timezone?

EDIT:

This is how my SQL query result looks like:

start: Mon Dec 28 2015 06:20:00 GMT+0100 (Central Europe Standard Time),

And this is after calling JSON.stringify:

"2015-12-28T05:20:00.000Z"

My computer is in GMT+0100. But I don't want any timezone information to be stored in JSON, neither in MySQL database. I get date from an external API in ISO 8601 format, but I don't want to store timezone information.


Solution

  • var db_config = {
         host     : 'localhost',
         user     : 'xxx',
         password : '',
         database : 'xxx',
         timezone: 'utc'  //<-here this line was missing
    };
    

    That timezone variable solved my problem. I found the answer here:NodeJS responded MySQL timezone is different when I fetch directly from MySQL