Try our conversational search powered by Generative AI!

Clearing a date field with code - You must enter a value between 01/01/1753 and 31/12/9999


When I try and set a DateTime field to DateTime.MinValue and save in code it gives me this error: You must enter a value between 01/01/1753 and 31/12/9999.

//Get the content reference of the page to update.
                ContentReference contentReference = new ContentReference(model.PropertyID);
                //Get the writeable version of the content reference.
                HeritagePropertyLandingPage propertyPage = _contentRepository.Get(contentReference).CreateWritableClone() as HeritagePropertyLandingPage;
                //Make the changes.
                propertyPage.EmergencyText.StartDateTime = DateTime.MinValue;

                _contentRepository.Save(propertyPage, EPiServer.DataAccess.SaveAction.Publish);

Works fine if I set it to a real date value, but I have the need to be able to clear this field out.

If I clear out the date field via the cms editor it allows me to do this.  When I then retrieve the value in code it is a DateTime.MinValue equivalent.

Any ideas?  I guess I could change the SaveAction to not validate, but then I don't see why Episerver itself via the editor lets me save a blank date, but I can't via code.

Mar 28, 2018 16:09

An option is to have your property declared as a nullable DateTime (DateTime?) instead, then you can assign the value null to it to clear it.

Mar 29, 2018 8:25

DateTime.MinValue is 00:00:00.0000000 UTC, January 1, 0001 which is fine to use in C#.

However SQL server doesn't support dates earlier than January 1, 1753, therefore they can't be saved.

As Johan said using DateTime? is the recommended way to handle it.

Mar 29, 2018 14:03

Thanks for the replies.  I just find it strange via the editor you can enter a blank date and it saves it ok.  So they must have logic there that sets a DateTime.MinValue to a null sql date.  That's how I've done this kind of thing before. 

I'm not sure I can risk changing the field strcuture now we have effectively live data.  If I change it do nullable I'm not sure the impact on all the existing records.

Mar 29, 2018 14:10

What does your definition of HeritagePropertyLandingPage look like?

Mar 29, 2018 14:26
It has this property:

            Name = "Emergency",
            GroupName = ContentGroupNames.Alerts,
            Order = 10)]
        public virtual EmergencyTextBlock EmergencyText { get; set; }

Which itself contains:

public class EmergencyTextBlock : BlockData
           Name = "Emergency text",
           GroupName = ContentGroupNames.Content)]
        public virtual string EmergencyText { get; set; }

          Name = "Display from",
          GroupName = ContentGroupNames.Content)]
        public virtual DateTime StartDateTime { get; set; }

          Name = "Display to",
          GroupName = ContentGroupNames.Content)]
        public virtual DateTime EndDateTime { get; set; }
Mar 29, 2018 15:07

A shot in the dark but can you test these two approaches:


For future reference i would like to recommend utilizing the built-in support for scheduled publish and expiration, if a block has expired there is no need to load it from the database before checking dates.

Mar 29, 2018 15:31

We do use  pubish and expiration for most dates, this is a special situation though.  Your answer worked perfect.  Cheers.

Apr 03, 2018 10:50
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.