[英]Why does MySql respond with a different time than is in the database?
So, from MySql workbench, the date is '2021-01-01 05:35:40', but when i fetch the date column through nodejs via the mysql npm package, I get 2021-01-01T10:52:48.000Z. 对我来说最奇怪的是,我创建此记录的本地日期实际上是 2021-01-01 00:35:40。 因此,我在工作台中获得的值提前 5 小时,我什至不知道如何阅读我在服务器上获得的响应(T 和 .000Z 位),但我认为它比我的提前 10 小时实际时间,比我从工作台得到的时间提前 5 小时。
这是答案。 这是因为您使用的是 NodeJS,或者更具体地说,是 JavaScript。 来自 MySQL 的日期在 JS 中被转换为 ISO 日期字符串,但它们的值保持不变。
您可以使用这些函数将日期从ISOdateStrings
为可读格式。
module.exports.formatDate = (date) => {
const _date = new Date(date);
const day = _date.getDate();
const month = _date.getMonth() + 1;
const year = _date.getFullYear();
return `${year}-${month}-${day}`;
}
module.exports.formatTime = (date) => {
const _date = new Date(date);
const hours = _date.getHours()
const minutes = _date.getMinutes();
const seconds = _date.getSeconds();
return `${hours}:${minutes}:${seconds}`;
}
module.exports.toDateTimestamp = (date) => {
const dateStamp = this.formatDate(date);
const timeStamp = this.formatTime(date);
return `${dateStamp} ${timeStamp}`
}
尝试以下步骤:
sudo /etc/init.d/mysqld 重启
参考 - https://major.io/2007/07/01/mysql-time-zone-different-from-system-time-zone/
default_time_zone=欧洲/里加
Also make sure you have updated mysql timezones via shell like this: https://dev.mysql.com/doc/refman/5.5/en/time-zone-support.html
using mysql_tzinfo_to_sql...
尝试以下步骤: 检查服务器和客户端的时区。 在正确的时区后重新启动 Sql 服务。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.