Adding a TimeSpan to a given DateTime
Asked Answered
P

8

31

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?

Pap answered 29/4, 2010 at 13:18 Comment(0)
U
71

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());
Unpredictable answered 29/4, 2010 at 13:20 Comment(0)
A
23

You need to change a line:

date = date.Add(t);
Architectonics answered 29/4, 2010 at 13:19 Comment(0)
M
15

dtb is right about DateTime being immutable. Think of it this way: a DateTime is a value type, which puts it in the same category as int or double. Instances of these structures cannot be modified; they can only be evaluated and copied.

Consider this code:

int i = 4;

i + 2;     // does not compile, but what if it did?
           // would i become 6? clearly not --
           // i + 2 expresses a NEW value, which can
           // be copied somewhere

i = i + 2; // there we go -- that's better

This is analogous to:

DateTime d = DateTime.Now;
TimeSpan t = TimeSpan.FromDays(1.0);

d.Add(t);     // compiles (because AddDays is a function),
              // but is really the same as i + 2 above

d = d.Add(t); // that's better

By the way, one thing that might help make this clearer is realizing that the above line, d = d.Add(t), is the same as d = d + t. And you wouldn't write d + t on its own line, just like you wouldn't write i + 2 on its own line.

Markley answered 29/4, 2010 at 13:27 Comment(1)
The real answer is always in the commentsBillings
S
7

A DateTime is immutable, but the Add and Subtract functions return new DateTimes for you to use.

DateTime tomorrow = DateTime.Now.AddDays(1);
Subcommittee answered 29/4, 2010 at 13:20 Comment(0)
A
6

What is wrong with just doing date = date.AddDays(1)?

Audsley answered 29/4, 2010 at 13:19 Comment(4)
Still has the same problem - DateTimes are immutable :)Chlorite
My Delta Time is a TimeSpan. So I can't use AddDays which takes an integer as a parameter.Pap
@Amokrane, if your delta is a timespan I think you should rephrase the question title.Audsley
@Amokrane, the larger point remains. .Add(...) returns a new date, it doesn't modify the existing date. You still would use date = date.Add(...Subcommittee
B
5

The result of date.Add(t) is what you're after:

 DateTime date = new DateTime(2010, 4, 29, 10, 25, 00);
 TimeSpan t = new TimeSpan(1, 0, 0, 0);

 // The change is here, setting date to be the *new* date produced by calling Add
 date = date.Add(t);

 Console.WriteLine("A day after the day: " + date.ToString());
Beers answered 29/4, 2010 at 13:20 Comment(0)
F
2
date.Add(t);

returns a modified DateTime and does not change the original instance on which you call the Add method on.

Fadden answered 29/4, 2010 at 13:22 Comment(0)
A
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);`
Anderer answered 14/6, 2017 at 7:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.