Alex Rivera | Logout

Subtract days from a DateTime

Asked 2012-06-22T07:33:07.903
212

I have the following code in my C# program.

DateTime dateForButton =  DateTime.Now;  
dateForButton = dateForButton.AddDays(-1);  // ERROR: un-representable DateTime

Whenever I run it, I get the following error:

The added or subtracted value results in an un-representable DateTime.
Parameter name: value

Iv'e never seen this error message before, and don't understand why I'm seeing it. From the answers Iv'e read so far, I'm lead to believe that I can use -1 in an add operation to subtract days, but as my question shows this is not the case for what I'm attempting to do.

Edit
Report

1 Answer

88

That error usually occurs when you try to subtract an interval from DateTime.MinValue or you want to add something to DateTime.MaxValue (or you try to instantiate a date outside this min-max interval). Are you sure you're not assigning MinValue somewhere?

answered 2012-06-22T07:46:28.780

Your Answer