cli icon indicating copy to clipboard operation
cli copied to clipboard

Sequelize How to set updateAt and createAt time zones

Open kkx600 opened this issue 3 years ago • 2 comments

What you are doing?

I used orm scaffolding sequelize-cli to build the model, my project timezone is incorrect always 8 hours less than the actual time, by setting timezone: +8:00, but the query data is still 8 hours less, the data in the database is correct.

image

image

image

// code here

"timezone": "+08:00"
模型的配置
createdAt: {
        allowNull: false,
        type: Sequelize.DATE,
        defaultValue: Sequelize.NOW
      },
      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE,
        defaultValue: Sequelize.NOW
      }

What do you expect to happen?

I want to set it to the same time as the local server

What is actually happening?

However, when I was developing and debugging locally, the time was normal, but after deploying in the production environment of Linux Ubuntu, my data time was wrong, always missing 8 hours Output, either JSON or SQL

Dialect: mysql __Database version:8.0 __Sequelize CLI version:6.4.1 __Sequelize version:6.17.0

kkx600 avatar May 11 '22 10:05 kkx600

19:00:14+8:00 and 11:00:14Z are the same time, they are displayed with different time offsets offsets because calling toJSON or toISOString on a Date object will return a string with a time offset of 0 (Z).

> new Date('2022-04-27 19:00:14+8:00')
2022-04-27T11:00:14.000Z
> new Date('2022-04-27 11:00:14Z')
2022-04-27T11:00:14.000Z

This sounds more like a misuse of the Date object than an error in converting the data when fetching from the database.

ephys avatar May 13 '22 14:05 ephys

19:00:14+8:00并且11:00:14Z是相同的时间,它们以不同的时间偏移量显示,因为在 Date 对象上调用toJSONtoISOString将返回一个时间偏移量为 0 ( Z) 的字符串。

> new Date('2022-04-27 19:00:14+8:00')
2022-04-27T11:00:14.000Z
> new Date('2022-04-27 11:00:14Z')
2022-04-27T11:00:14.000Z

这听起来更像是对Date对象的滥用,而不是从数据库中获取数据时转换数据时的错误。

我使用的是Sequelize脚手架默认的时间NOW方法,去填充的时间,按常理应该没问题,但是取出来就少了8小时

kkx600 avatar May 13 '22 14:05 kkx600