在我的 Node 应用程序中,我有 MySQL 数据库,其中有一个名为 xxx_date 的 GMT 日期格式的列。我正在使用 Node JS 从数据库查询日期字段。结果数据采用我的本地时间格式。我想要相同的数据(格林尼治标准时间)。我也尝试使用 CONVERT_TZ 方法更改查询。但结果数据不匹配。有人提出了一种正确的方法。
例如
xxx_date in DB: 2017-05-22 08:14:00
the date I get after querying from DB: 2017-05-22T02:44:00.000Z
but it should be either: 1:45 PM Monday, May 22, 2017, or 2017-05-22 08:14:00
这背后发生了什么?
最佳答案
在谷歌搜索一天后,我发现了这个。 MySQL连接将默认使用本地时区( https://github.com/sidorares/node-mysql2/issues/262 ),通过在配置文件中将其更改为UTC,我们可以获取与数据库中相同的数据。
在mysql连接中
var con = mysql.createConnection({
host: "localhost",
user: "yourusername",
password: "yourpassword",
timezone:'UTC',
port: 3307
});
关于javascript - Node js在查询记录时自动将MySQL中的GMT格式日期转换为本地格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44177482/