DateTime dateForButton = DateTime.Now.AddDays(-1);
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?
You can do:
DateTime.Today.AddDays(-1)
You can use the following code:
dateForButton = dateForButton.Subtract(TimeSpan.FromDays(1));
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);
I've had issues using AddDays(-1).
My solution is TimeSpan.
DateTime.Now - TimeSpan.FromDays(1);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With