我正在使用 node 和 sequalize 来创建 Web API。一切正常,但是当我按如下方式更新 user
表时:
var localDate=getDateTime();
console.log(localDate)
//output: 2021/06/03 18:01:22 this datetime is in local formate(GMT+0500),exactly which I need in `MySQL`
var data = { ActivationDate:localDate};
User.update(data, {where: { Email: req.body.email }}).then(num=>{/*whatever*/})
数据库已更新,但在 MySQL
中 ActivationDate
表的 user
列根据 UTC
进行更新,即 2021-06-03 13:01:22
,我需要根据 local formate(GMT+0500) 2021/06/03 18:01:22
进行更新。为了防止这种行为,I also search from here, but these solutions are not working for me.
我还在 config.js 文件中添加以下内容:
dialectOptions: {
useUTC: false, //for reading from database
dateStrings: true,
typeCast: function (field, next) { // for reading from database
if (field.type === 'DATETIME') {
return field.string()
}
return next()
},
},
timezone: '+05:00'
但是,ActivationDate
仍然是根据 UTC 存储的。如何在
ActivationDate
列中保存我的本地 DateTime。谢谢。
最佳答案
MySQL,对吗?
在您的选项中尝试 timezone: 'Asia/Karachi'
。
MySQL 中的 TIMESTAMP
项始终以 UTC 存储,即使您在本地时区提供它们。它们始终转换为您选择的时区。DATETIME
项始终存储在本地时间。
关于javascript - 我无法使用 sequelize Node 在 MySQL 中设置本地 "timezone",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67822637/