Save datetime from C # to MongoDB

I am trying to store a Datetime value from C # to MongoDB with the value:

DateTime.ParseExact("10/02/2015", "dd/MM/yyyy", CultureInfo.InvariantCulture)

      

But MongoDB shows up:

ISODate("2015-02-09T17:00:00.000Z")

      

I don't know why MongoDB comes out later than one day (date: 9) and my date is 10. Thanks for reading my question.

Update 1: As #mnemosyn's answer, I am doing some changes:

DateTime.SpecifyKind((DateTime.ParseExact("20/07/2015", "dd/MM/yyyy", CultureInfo.InvariantCulture)), DateTimeKind.Utc)

      

Result: ISODate("2015-07-20T00:00:00.000Z")

Read more: DateTime.ToLocalTime Method

+3


source to share


1 answer


The problem is that the parsed date is not considered a UTC date. 02-09 + 7h exactly 02-10 ...

DateTime

has a property Kind

that is of type DateTimeKind

. It can be Local

, UTC

or Unspecified

. If the string you are processing does not indicate what it is, the returned one DateTime.Kind

will beUnspecified

. The MongoDB driver then converts that to UTC because that's more often than not what people expect when they think of DateTime

.



Note that the mantra "always keeps UTC in the database" is not always correct, for example. for the bus schedule.

+6


source







All Articles