简体   繁体   中英

Node.js + MySQL: Time is 2 hours off

I'm saving a date / time in MySQL using PhpMyAdmin. When i look at the date in MySQL it says 21:00 Then i look at the output in node.js, and the time is 19:00.

I'm doing a simple query:

connection.query("select * from mytable WHERE id = ?" , [id], function(err,rows){
    if(!err) {
        res.json(rows);
    }           
});

Any hints as to what is causing this?

Trying inputting this line on top of your node.js

process.env.TZ = 'UTC';

You want to try to make timezone uniform across your stack.
It might be the case that timezones on node vs. mysql might be one factor. try setting both to the same timezone, preferably UTC

Also, take a look at your mysql config:

var db_config = {
  host     : 'localhost',
  user     : 'xxx',
  password : '',
  database : 'xxx',
  timezone: 'utc'  // have you set this?
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM