简体   繁体   English

Node.js + MySQL:时间缩短2小时

[英]Node.js + MySQL: Time is 2 hours off

I'm saving a date / time in MySQL using PhpMyAdmin. 我正在使用PhpMyAdmin在MySQL中保存日期/时间。 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. 当我查看MySQL中的日期时,它显示为21:00,然后查看node.js中的输出,时间为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 尝试在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. 节点与mysql上的时区可能是一个因素。 try setting both to the same timezone, preferably UTC 尝试将两者都设置为相同的时区,最好是UTC

Also, take a look at your mysql config: 另外,看看您的mysql配置:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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