Mongodb date is off by 1 hour

岁酱吖の 提交于 2019-12-21 20:36:22

问题


I am running mongodb on ubuntu server. The server time is

root# date
Thu Sep 13 21:15:58 BST 2012

But when I run the following command I get a different result

root# mongo
MongoDB shell version: 2.2.0
connecting to: test
> new Date()
ISODate("2012-09-13T20:15:58.670Z")

There is exactly one hour difference. When I update a documents updated_on field with php using MongoDate(), the value of the field is still 1 hour off.

[EDIT] Actually I just checked my php error log and the time in the log file is 1 hour off as well

[13-Sep-2012 20:11:14 UTC] Log Message (Time should be 21:11:14)

回答1:


Mongo tells you

2012-09-13T20:15:58.670Z

Z = Zulu time / Zero offset / UTC. You can also express the time in that TZ as 2012-09-13T20:15:58.670+00:00, as defined in the ISO8601 standard by the way.

BST is UTC+1. So, they are the same time but in different time zones.




回答2:


You can resolve this issue by displaying the DateTime with ToLocalTime method.

MVC C# Example: @Model.StartDate.ToLocalTime()

This is due to the way MongoDB store datetime in BST format. So the daylight savings time or the time zone of the server will have an effect on the actual date time returned to the application. This simple code will be able to format as usual with ToString("dd MMMM yyyy hh:mm tt") or any other format based on your requirements.




回答3:


Here you need to understand a concept in time setting in clocks called daylight saving time. In some countries around the world the clock is advanced by 1 or more hours to experience day light by one more hour. The difference between IST and GST is 5.30 hrs but the actual time difference is between New Delhi and London time is 6.30 hrs. See this article from 4GuysFromRolla for setting and using server time.



来源:https://stackoverflow.com/questions/12414158/mongodb-date-is-off-by-1-hour

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!