Click here to Skip to main content
14,973,981 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

Updated 7-Jun-18 3:01am

1 solution

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
Mukesh Pr@sad 7-Jun-18 9:15am
Thanks..trying this solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900