将一时间戳字段默认值设为0001-01-01 00:00:00 UTC,在timezone为东8区的数据库中字段值显示为:
0001-01-01 08:05:43+08:05:43
Google后找到如下解释:
0001-01-01 00:00:00 UTC. And that is indeed 0001-01-01 08:05:43 in your local time zone (whatever that may be, probably Asia/Shanghai
or somewhere near that). When you console.log
a Date
, it displays the date in your local time zone. The Date
value is correct. It's just displayed in a different format.
The extra 5 minutes and 43 seconds is because at the year AD 1, time zones have not been standardised, and the local mean time offset from UTC at your location is +08:05:43.
也就是说在0001年的时候还没有标准化时区。经自行测试发现标准化时区是从1901年1月1日开始的,之前都是LMT(Local Mean TIme):
1900-01-01 00:00:00 UTC --> 1900-01-01 08:05:43+08:05:43
1901-01-01 00:00:00 UTC --> 1901-01-01 08:00:00+08
所以想避免为时间显示做额外的处理,可将默认时间设为1901-01-01 00:00:00 UTC。
通常应该将数据库的时区设置为UTC,在需要前端展示的时候将其转化为当地时区,这样就可以将时间戳字段默认值设为0001-01-01 00:00:00 UTC。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了