I want to add days in some date. I have a code like this:
DateTime endDate = Convert.ToDateTime(this.txtStartDate.Text); Int64 addedDays = Convert.ToInt64(txtDaysSupp.Text); endDate.AddDays(addedDays); DateTime end = endDate; this.txtEndDate.Text = end.ToShortDateString();
But this code is not working, days are not added! What the stupid mistake I'm doing?
A DateTime function performs an action or calculation on a date and time value. Use a DateTime function to add or subtract intervals, find the current date, find the first or last day of the month, extract a component of a DateTime value, or convert a value to a different format.
The DateTime value type represents dates and times with values ranging from 00:00:00 (midnight), January 1, 0001 Anno Domini (Common Era) through 11:59:59 P.M., December 31, 9999 A.D. (C.E.) in the Gregorian calendar. Time values are measured in 100-nanosecond units called ticks.
DateTime is immutable. That means you cannot change it's state and have to assign the result of an operation to a variable.
endDate = endDate.AddDays(addedDays);
You need to catch the return value.
The DateTime.AddDays method returns an object who's value is the sum of the date and time of the instance and the added value.
endDate = endDate.AddDays(addedDays);
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