Sequelize —将UNIX时间戳记用于DATE字段 [英] Sequelize — use UNIX timestamp for DATE fields

查看:629
本文介绍了Sequelize —将UNIX时间戳记用于DATE字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法可以强制Sequelize使用UNIX时间戳作为默认时间格式两者(对于createdAt/updatedAt时间戳)用于自定义Sequelize.DATE字段类型? >

谢谢!

P.S.我正在使用MySQL

解决方案

在任何给定的时刻,都有两个可能的日期(取决于相对于国际日期线的位置):即从UNIX时间戳转换约会需要一个人考虑时区.

例如,UNIX时间戳9466848002000-01-01 00:00:00Z.尽管这代表着新千年的第一天,但​​在大西洋以东的任何地方,它仍然是千禧年的前夕,在大西洋以西的任何地方.那么它代表哪个日期?

虽然可以从日期转换为时间戳,但必须定义自己的约定(例如,将给定日期表示为UTC的午夜),否则可以表示相同的日期每种编码方式都不同.一般来说,这是一个坏主意,可能会产生各种意想不到的后果.

存在DATE数据类型的原因是:这是 存储日期的正确方法.使用它.

Is there a way to force Sequelize use UNIX Timestamp as default time format both for createdAt/updatedAt timestamps and for custom-defined Sequelize.DATE field types?

Thanks!

P.S. I'm using MySQL

解决方案

At any given moment in time, there are two possible dates (depending on one's position relative to the international date line): that is, converting from a UNIX timestamp to a date requires one to consider the timezone.

For example, the UNIX timestamp 946684800 is 2000-01-01 00:00:00Z. Whilst this represents the first day of the new millenium pretty much everywhere east of the Atlantic, it's still millenium eve everywhere to the west of that ocean. So which date does it represent?

Whilst it's possible to convert from a date to a timestamp, one must define one's own convention for so doing (e.g. represent a given date as midnight in UTC) or else the same date may be represented differently upon each encoding. Generally speaking, this is a bad idea which may have all sorts of unintended consequences.

There is a reason that the DATE data type exists: it is the correct way to store a date. Use it.

这篇关于Sequelize —将UNIX时间戳记用于DATE字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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