【问题标题】:I am unable to set local "timezone" in MySQL using sequalize node我无法使用 sequelize 节点在 MySQL 中设置本地“时区”
【发布时间】:2021-08-21 15:30:57
【问题描述】:

我正在使用 nodesequalize 来创建 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*/})

数据库更新了,但是在MySQLuser表的ActivationDate列是根据UTC更新的,也就是2021-06-03 13:01:22,我需要根据本地甲酸(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 列中。

谢谢。

【问题讨论】:

    标签: javascript mysql node.js datetime sequelize.js


    【解决方案1】:

    MySQL,对吗?

    在您的选项中尝试timezone: 'Asia/Karachi'

    TIMESTAMP MySQL 中的项目始终以 UTC 存储,即使您在本地时区提供它们也是如此。它们始终会转换为您选择的时区。

    DATETIME 项目始终以本地时间存储。

    【讨论】:

    • 根据您的回答“MySQL 中的 TIMESTAMP 项目始终以 UTC 存储,即使您在本地时区提供它们”,这意味着无法根据本地时间存储在 DateTime MySql 中的区域?
    • 另一方面,我在选项中添加了 timezone: 'Asia/Karachi',但 DateTime 仍然根据 UTC 时区存储。
    猜你喜欢
    • 2021-09-11
    • 1970-01-01
    • 2018-07-30
    • 1970-01-01
    • 2015-09-12
    • 1970-01-01
    • 2018-07-19
    • 2018-02-14
    • 1970-01-01
    相关资源
    最近更新 更多