Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Datetime in C# add days

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?

like image 807
Nomi Ali Avatar asked Mar 12 '13 11:03

Nomi Ali


People also ask

What is the DateTime function?

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.

What is DateTime value?

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.


2 Answers

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); 
like image 150
Steve Avatar answered Sep 30 '22 14:09

Steve


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); 
like image 29
Jensen Avatar answered Sep 30 '22 13:09

Jensen