Sequelize仅读取UTC中的日期时间 [英] Sequelize reads datetime in UTC only

查看:537
本文介绍了Sequelize仅读取UTC中的日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将timezone选项设置为我的时区(Europe/Zagreb,我也尝试过使用+02:00),并且保存了应该保存的时间,但是,从数据库中读取时间时, Sequelize将其转换为UTC.我也尝试过不同的时区,每个时区都可以正确保存,但是从数据库读取时总是转换为UTC.

I have set the timezone option to my timezone (Europe/Zagreb and I've tried with +02:00 too), and the time is saved as it should be, however, when reading the time from the database, Sequelize converts it to UTC. I have tried different timezones too, each is saved correctly, but always converted to UTC when read from database.

我做错了什么吗?或者这是一个已知问题,是否有任何解决方法?

Am I doing something wrong or is this a known issue and are there any workarounds?

我使用以下方法创建新连接:

I create a new connection with:

sequelize = new Sequelize(config.database, config.username, config.password, config);

我的配置如下所示:

"development": {
    "username": "root",
    "password": "root",
    "database": "db",
    "host": "localhost",
    "dialect": "mysql",
    "timezone": "Europe/Zagreb"
}

推荐答案

您可以尝试这段代码,我遇到了同样的问题,并且对我有用.

You can Try This code, I had the same problem and this worked for me.

const sequelize = new Sequelize(mysql.database, mysql.user, mysql.password, {
    host: mysql.host,
    port:3306,
    dialect:'mysql',
    define: {
      underscored: true,
      freezeTableName: true, //use singular table name
      timestamps: false,  // I do not want timestamp fields by default
    },
    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: '+01:00'
});

这篇关于Sequelize仅读取UTC中的日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆