Get DateTime from mongo collection using C # driver

I have the following object:

public class QueueItem { public long _id { get; set; } public SiteMapRequestState State { get; set; } public int WorkerId { get; set; } public DateTime QueuedTime { get; set; } public DateTime StartWorkTime { get; set; } } 

When I save the object in mongo, the date is displayed correctly (DateTime fields contain the correct date that I inserted). However, when I pull this object, I get a different time (-3 hours).

For example: I save an object using StartWorkTime = 6/26/2012 10:00:00 AM , then when I get an object from the collection, StartWorkTime is 6/26/2012 7:00:00 AM . This makes the process time calculation incorrect.

I use the latest C # driver (1.4.2.4500) and run both the code and the mongo server on the same computer.

What could be the difference?

Thanks!

+4
source share
2 answers

After digging a bit, I found this message: http://alexmg.com/post/2011/09/30/DateTime-precision-with-MongoDB-and-the-C-Driver.aspx

http://www.mongodb.org/display/DOCS/CSharp+Driver+Serialization+Tutorial#CSharpDriverSerializationTutorial-DateTimeSerializationOptions

I needed to mark the DateTime property with the attribute [BsonDateTimeOptions (Kind = DateTimeKind.Local)]

+7
source

I had this question once. Remember to also specify the DateTimeKind parameter for DateTimeKind.Utc

Hope this helps.

0
source

All Articles