Datetime in C# add days

68

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?

This question is tagged with c# date c#-4.0 datetime add

~ Asked on 2013-03-12 11:27:40

The Best Answer is


119

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);

~ Answered on 2013-03-12 11:30:34


17

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);

~ Answered on 2013-03-12 11:28:35


Most Viewed Questions: