[英]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.