[英]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. 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. The weirdest thing to me, is the local date I did created this record would've actually been 2021-01-01 00:35:40.
对我来说最奇怪的是,我创建此记录的本地日期实际上是 2021-01-01 00:35:40。 So, the value I am getting in workbench is 5 hours ahead, and I don't even know how to read the response I am getting on my server(T and.000Z bit) but I assume that it is 10 hours ahead of my actual time, and 5 hours ahead of what I am getting from workbench.
因此,我在工作台中获得的值提前 5 小时,我什至不知道如何阅读我在服务器上获得的响应(T 和 .000Z 位),但我认为它比我的提前 10 小时实际时间,比我从工作台得到的时间提前 5 小时。
Here's the answer.这是答案。 It is because you are using NodeJS or more specifically, JavaScript.
这是因为您使用的是 NodeJS,或者更具体地说,是 JavaScript。 Dates from MySQL get converted to ISO date strings in JS but their value remains the same.
来自 MySQL 的日期在 JS 中被转换为 ISO 日期字符串,但它们的值保持不变。
You can use these functions to format dates from ISOdateStrings
to readable formats.您可以使用这些函数将日期从
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}`
}
Try the following steps:尝试以下步骤:
sudo /etc/init.d/mysqld restart
sudo /etc/init.d/mysqld 重启
Reference - https://major.io/2007/07/01/mysql-time-zone-different-from-system-time-zone/参考 - https://major.io/2007/07/01/mysql-time-zone-different-from-system-time-zone/
default_time_zone=Europe/Riga
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 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...
Try the following steps: Check timezone of server and your client end.尝试以下步骤: 检查服务器和客户端的时区。 Restart Sql service after correct Time zone.
在正确的时区后重新启动 Sql 服务。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.