Mysql VS MongoDB 时间戳转年月日时分秒

关于MongoDB中的时间戳

> db.foo.find()
{ "_id" : ObjectId("5a7140013469a2e6d90d528a"), "created_at" : 1512057733000 }

> db.foo.aggregate(
... {$project:{created_at:{"$add": [ new Date(0), "$created_at" ]}}}
... )
{ "_id" : ObjectId("5a7140013469a2e6d90d528a"), "created_at" : ISODate("2017-11-30T16:02:13Z") }

但是同样的时间戳在Mysql中却又不同了

>select from_unixtime(1512057733);
+---------------------------+
| from_unixtime(1512057733) |
+---------------------------+
| 2017-12-01 00:02:13       |
+---------------------------+

到底哪个更准呢?

阅读 4.7k
1 个回答

两个都是准确的,你没有考虑时区的问题。MongoDB存储的是GMT0时间,MySQL给你的是服务器时区(中国区),也就是GMT+8。算一下正好差8小时,都没有错。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题