Datetime in C# add days

Nomi Ali picture Nomi Ali · Mar 12, 2013 · Viewed 199.2k times · Source

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?

Answer

Darren Young picture Darren Young · Mar 12, 2013

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