将一时间戳字段默认值设为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。