Sequelize 仅以 UTC 格式读取日期时间

Sequelize reads datetime in UTC only(Sequelize 仅以 UTC 格式读取日期时间)
本文介绍了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);

我的配置看起来像这样:

and my configuration looks something like this:

"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 格式读取日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)
SQL/MySQL: split a quantity value into multiple rows by date(SQL/MySQL:按日期将数量值拆分为多行)