mongo db中的时间问题 [英] Time issue in mongo db

查看:116
本文介绍了mongo db中的时间问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨专家,



当我在mongo db中插入日期时它正确插入

ex: - CreatedDate:2018-06 -07 2:51:46.925



在xamarin中检索时出现问题

ex:CreatedDate:2018-06-07 9: 21:46.925



这意味着它会减少5小时30分钟。



什么我试过了:



我试图将5小时30分钟添加到检索到的时间,这样可以给出正确的价值

但这不是任何人告诉我的正确方法,为什么会发生这种情况以及解决方案是什么。



谢谢

解决方案

这是与UTC的印度标准时间偏移量。



日期时间内部始终以UTC格式存储在MongoDB中。为避免出现问题,您应确保在存储时日期为UTC,或者 - 当作为文本传递时 - 设置相应的时区信息(然后将它们转换为UTC)。



类似适用于检索日期。它们将采用UTC格式,并可选择转换为当地时间进行显示。如何做到这一点取决于使用的编程语言。



在你的情况下,你在存储和/或检索时没有这样做。请自行检查您的代码或在问题中添加相关部分以获得帮助。



一般规则是:
  • 始终使用数据库日期时间格式存储(从不作为字符串)

  • 始终以UTC格式存储(MongoDB必需)

  • 尽可能使用二进制日期时间对象(避免字符串和转换)

  • 如果显示使用编程语言函数或SQL格式选项需要本地时间

  • 如果在远程服务器(例如Web服务器)上完成到本地时间的转换,则客户端时区信息必须存储在数据库中并检索以进行转换


Hi Experts,

When I am inserting datetime in mongo db it is inserting correctly
ex:- CreatedDate:2018-06-07 2:51:46.925

while retrieving in xamarin it is coming wrong
ex: CreatedDate:2018-06-07 9:21:46.925

that means it is decreasing by 5 hours 30 minutes.

What I have tried:

I tried to add 5 hours 30 minutes to the retrieved time , which is giving correct value
but it is not the correct way can anyone tell me, why it is happening and what is the solution.

thanks

解决方案

That is the India Standard Time offset against UTC.

The datetime is stored internally always in UTC with MongoDB. To avoid problems, you should ensure that dates are in UTC when storing or - when passed as text - have the corresponding time zone information set (they will be then converted to UTC).

Similar applies when retrieving dates. They will be in UTC and can be optionally converted to local time for display. How to do that depends on the used programming language.

In your case you have not done so while storing and/or retrieving. Check your code yourself or add the relevant portions to your question to get help.

General rules are:
  • Always store using a database datetime format (never as string)
  • Always store in UTC (mandatory with MongoDB)
  • Use binary datetime objects whenever possible (avoid strings and conversions)
  • If local time is required for display use programming language functions or SQL formatting options
  • If the conversion to local time is done on a remote server (e.g. a web server), the clients time zone information must be stored in the database and retrieved for conversion


这篇关于mongo db中的时间问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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