Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

 date.Add(t);

 Console.WriteLine("A day after the day: " + date.ToString());

I thought the result would be: 2010 04 30- 10:25:00 but I'm still getting the initial date.

What's wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
440 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...