节点脚本查询时数据库中的日期不匹配



我有一个节点脚本,它创建一个日期对象,将其转换为字符串,控制台将日期和日期记录为字符串,然后将日期字符串插入到类型为 DATE、DATETIME 和 TIMESTAMP 的 sql 数据库字段中。该数据库位于 XAMPP 的 Windows 10 上,当地时间为下午 2 点。

(async () => {
const fs = require("fs");
const mysql = require('mysql2/promise');
let date = new Date();
let dateString = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();
console.log("date: ", date);
console.log("datestring: ", dateString);
const connection = await mysql.createConnection(JSON.parse(fs.readFileSync("../test/databaseLogin.json")));
await connection.execute("INSERT INTO dates VALUES (?, ?, ?, ?)", [1, dateString, dateString, dateString]);
const selectResults = await connection.execute("SELECT * FROM `dates` WHERE mainKey = ?", [1]);
console.log("DB field of type DATE: ", selectResults[0][0].date);
console.log("DB field of type DATETIME: ", selectResults[0][0].datetime);
console.log("DB field of type TIMESTAMP: ", selectResults[0][0].timestamp);
connection.close();
})();

如果我使用 mysql2 npm 从节点进程查询数据库,我会得到不同的结果,显示第 24 个而不是第 25 个。 完整的控制台输出:

date:  2019-12-25T13:19:01.204Z
datestring:  2019-12-25
DB field of type DATE:  2019-12-24T23:00:00.000Z
DB field of type DATETIME:  2019-12-24T23:00:00.000Z
DB field of type TIMESTAMP:  2019-12-24T23:00:00.000Z

我不明白为什么会有区别。也许是某种本地化问题?但是日期是在读取时相同的区域设置中创建的。

数据的数据库表示形式:

MariaDB [test]> SELECT *  FROM dates;
+---------+------------+---------------------+---------------------+
| mainKey | date       | datetime            | timestamp           |
+---------+------------+---------------------+---------------------+
|       1 | 2019-12-25 | 2019-12-25 00:00:00 | 2019-12-25 00:00:00 |
+---------+------------+---------------------+---------------------+
MariaDB [test]> describe dates;
+-----------+-----------+------+-----+-------------------+-----------------------------+
| Field     | Type      | Null | Key | Default           | Extra                       |
+-----------+-----------+------+-----+-------------------+-----------------------------+
| mainKey   | int(11)   | NO   | PRI | NULL              |                             |
| date      | date      | NO   | PRI | NULL              |                             |
| datetime  | datetime  | NO   |     | NULL              |                             |
| timestamp | timestamp | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+-----------+-----------+------+-----+-------------------+-----------------------------+

MySQL将时间戳值从当前时区转换为UTC以进行存储,并从UTC转换回当前时区以进行检索。(对于其他类型(如 DATETIME(,不会发生这种情况。默认情况下,每个连接的当前时区是服务器的时间。可以基于每个连接设置时区。只要时区设置保持不变,您就会得到与存储相同的值。

我认为 mysql2 npm 不支持每个连接的时区配置。

我在访问 mysql2 npm 模块的问题页面时找到了答案。

为了从数据库中"按原样"获取日期字符串,我必须添加以下连接设置:dateStrings: true.完整连接对象现在如下所示:

const connection = await mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'pass',
database: 'test',
dateStrings: true
});

控制台输出:

date:  2019-12-25T20:38:09.522Z
datestring:  2019-12-25
DB field of type DATE:  2019-12-25
DB field of type DATETIME:  2019-12-25
DB field of type TIMESTAMP:  2019-12-25

最新更新