Main Site Documentation

DateTime.AddDay Related method are not work


#1

I try this simple code

DateTime t1 = DateTime.Now;
            DateTime t = DateTime.Now;

            t.AddDays(20);
            Debug.Print(t1.ToString());
            Debug.Print(t.ToString());

But the Debug.output is the same.

06/01/2011 00:01:32
06/01/2011 00:01:32

I try both in spider and Bee.

If this methods not works how to change the time??

Thanks


#2

AddDays and other DateTime methods return new instance.

try this:



DateTime t1 = DateTime.Now;
            DateTime t = DateTime.Now;
 
            t1 = t.AddDays(20);
            Debug.Print(t1.ToString());
            Debug.Print(t.ToString());



#3

t.AddDays(20) doesn’t add days to t, it returns a new DateTime object

replace this:

t.AddDays(20);

with this:

t = t.AddDays(20);


#4

@ NickP - How is that different from the post above? :wink:


#5

Thank you Architect and NickP.
Ha, so simple answer, I am stupid.
It waste me all day. I should post early.

Thank you.


#6

I am sure We all had moments like that. Sometimes a second pair of eyes is all that is needed. :wink: