Subtract days from a DateTime

C#Datetime

C# Problem Overview


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.

C# Solutions


Solution 1 - C#

DateTime dateForButton = DateTime.Now.AddDays(-1);

Solution 2 - C#

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?

Solution 3 - C#

You can do:

DateTime.Today.AddDays(-1)

Solution 4 - C#

You can use the following code:

dateForButton = dateForButton.Subtract(TimeSpan.FromDays(1));

Solution 5 - C#

The dateTime.AddDays(-1) does not subtract that one day from the dateTime reference. It will return a new instance, with that one day subtracted from the original reference.

DateTime dateTime = DateTime.Now;
DateTime otherDateTime = dateTime.AddDays(-1);

Solution 6 - C#

Instead of directly decreasing number of days from the date object directly, first get date value then subtract days. See below example:

DateTime SevenDaysFromEndDate = someDate.Value.AddDays(-1);

Here, someDate is a variable of type DateTime.

Solution 7 - C#

I've had issues using AddDays(-1).

My solution is TimeSpan.

DateTime.Now - TimeSpan.FromDays(1);

Solution 8 - C#

The object (i.e. destination variable) for the AddDays method can't be the same as the source.

Instead of:

DateTime today = DateTime.Today;
today.AddDays(-7);

Try this instead:

DateTime today = DateTime.Today;
DateTime sevenDaysEarlier = today.AddDays(-7);

Solution 9 - C#

Using AddDays(-1) worked for me until I tried to cross months. When I tried to subtract 2 days from 2017-01-01 the result was 2016-00-30. It could not handle the month change correctly (though the year seemed to be fine).

I used date = Convert.ToDateTime(date).Subtract(TimeSpan.FromDays(2)).ToString("yyyy-mm-dd"); and have no issues.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBuenaView Question on Stackoverflow
Solution 1 - C#Christian PhillipsView Answer on Stackoverflow
Solution 2 - C#CyberDudeView Answer on Stackoverflow
Solution 3 - C#samView Answer on Stackoverflow
Solution 4 - C#Rajesh SubramanianView Answer on Stackoverflow
Solution 5 - C#cahit beyazView Answer on Stackoverflow
Solution 6 - C#Sambhav jainView Answer on Stackoverflow
Solution 7 - C#UsernameNotFoundView Answer on Stackoverflow
Solution 8 - C#Mark BensonView Answer on Stackoverflow
Solution 9 - C#Ainsley HobartView Answer on Stackoverflow