How to save date properly?
Asked Answered
R

6

27

I'm trying to save date (using C# official driver):

val = DateTime.Parse(value).Date; //Here date is {11/11/2011 12:00:00 AM}
var update = Update.Set("Date", val);
...

When I select Date from the database, the value is {11/10/2011 10:00:00 PM}

How to save only the date I want?

Rev answered 9/11, 2011 at 10:5 Comment(0)
A
47

c# driver by default (without extra settings) saving local dates as utc date into database (date - time zone offset) but reading back without any action (so, utc date).

Because of this when you loading datetime from database you receive diff in 2 hours (your timezone offset). There are two approaches how to say to mongodb c# driver convert utc dates to local timezone dates during deserialization:

1.through the attributes for particular date field:

[BsonDateTimeOptions(Kind = DateTimeKind.Local)]
public DateTime SomeDateProperty {get;set;}

2.through global settings for all datetime fields (default is UtcInstance):

DateTimeSerializationOptions.Defaults = DateTimeSerializationOptions.LocalInstance;

Once you will do #1 or #2 you will see local date.

Update:


#2 is obsolete in latest driver version so use code below instead:

BsonSerializer.RegisterSerializer(typeof(DateTime), 
             new DateTimeSerializer(DateTimeSerializationOptions.LocalInstance));

Update:


#2 has changed again:

BsonSerializer.RegisterSerializer(typeof(DateTime), DateTimeSerializer.LocalInstance);
Alver answered 9/11, 2011 at 12:23 Comment(5)
I tried the second solution and got the following warning: "MongoDB.Bson.Serialization.Options.DateTimeSerializationOptions.Defaults is obsolete: Create and register a DateTimeSerializer with the desired options instead."Codycoe
Using the RegisterSerializer in my unit test project I get this error: SetUp : MongoDB.Bson.BsonSerializationException : There is already a serializer registered for type System.DateTime. It works only the first timeVerile
@Verile #21386847Hamo
Isn't it just easier to save the date as a double (seconds) UTC in mongo and then do the translation on the client side when your pulling it out?Ideomotor
while using [BsonDateTimeOptions(Kind = DateTimeKind.UTC)] still converts my date to UTCParalogism
P
3

You're running into a timezone issue. Your date object is probably in a timezone other than UTC (2 hours ahead by the looks of it) or your default timezone is set to something other than UTC. The driver will convert the date to the appropriate timezone before storing it in the database.

Normally you wouldn't notice this since the reverse (retrieving the UTC date from the database) should convert it back to the original timezone. There might be an issue with the driver you're using or C# might require a bit more code to get it right.

Storing dates as strings is usually not a good idea since it disables range queries on dates.

Pullen answered 9/11, 2011 at 10:31 Comment(0)
T
1

Mongo stores everything in UTC, in case your date time is UTC this will help

val = DateTime.SpecifyKind(val , DateTimeKind.Utc);
var update = Update.Set("Date", val);
Tarrant answered 17/11, 2014 at 1:16 Comment(0)
D
1

2.2.4.26 has changed again:

BsonSerializer.RegisterSerializer(typeof(DateTime), DateTimeSerializer.LocalInstance);
Dailey answered 13/8, 2016 at 16:54 Comment(0)
P
0

In my case [BsonDateTimeOptions(Kind = DateTimeKind.Local)] doesn't worked.

What i did is below

    DateTime _someDateProperty ;
    public DateTime SomeDateProperty 
    {
        get { return _someDateProperty ; }
        set
        {
            _someDateProperty = new DateTime(value.Ticks, DateTimeKind.Local);
        }
    }
Paralogism answered 18/3, 2019 at 10:4 Comment(0)
C
-1

Mongodb Date value stored in "UTC DateTime" format which comprises of both date & time. so you just cannot store date alone in the field. Instead you can get the date part alone in your application code after retrieving from mongo.

like in c#

datevalue.ToString("MM/dd/yyyy");

or

datevalue.ToShortDateString() 
Chaker answered 9/11, 2011 at 10:16 Comment(1)
This doesn't work when getting midnight dates. During daylight savings they get moved back a day to 11pm the previous night...Mill

© 2022 - 2024 — McMap. All rights reserved.