I just want to add 1 day to a DateTime
. So I wrote:
DateTime date = new DateTime(2010, 4, 29, 10, 25, 00);
TimeSpan t = new TimeSpan(1, 0, 0,
DateTime wont work if DateTime obj datatype is "DateTime?" which takes accepts null values, in such case DateTime? dt = DateTime.Now;
DateTime dateObj = new DateTime();
dateObj = Convert.ToDateTime(dt.ToString());
var Month3 = dateObj.AddMonths(3);`
You need to change a line:
date = date.Add(t);
A DateTime is immutable, but the Add and Subtract functions return new DateTimes for you to use.
DateTime tomorrow = DateTime.Now.AddDays(1);
DateTime values are immutable. The Add method returns a new DateTime value with the TimeSpan added.
This works:
Console.WriteLine("A day after the day: " + date.Add(t).ToString());
date.Add(t);
returns a modified DateTime and does not change the original instance on which you call the Add method on.
What is wrong with just doing date = date.AddDays(1)
?