PDA

Click to See Complete Forum and Search --> : [RESOLVED] subtract a year from datetime.now


Crash893
Jul 22nd, 2007, 03:12 PM
Hi all

I am trying to make a string that i can put into a sql query that will retrive one years worth of records

i have the code

System.DateTime date2 = new System.DateTime();
date2 = DateTime.Now;
textBox1.Text = date2.ToString("MM/dd/yyyy HH:MM:ss");
date2.AddYears(-1);
textBox2.Text = date2.ToString("MM/dd/yyyy HH:MM:ss");

but it just doesnt seem to be working so im thinking my addyears(-1) is wrong

can anyone give me a hint to point me in the right direction?

nmadd
Jul 22nd, 2007, 03:27 PM
.AddYears(-1) is correct. The problem is this line:
date2.AddYears(-1);
What are you doing with the result of this line? It just disappears. ;)



DateTime date2 = DateTime.Now;
this.textBox1.Text = date2.AddYears(-1).ToString("MM/dd/yyyy HH:MM:ss");

szlamany
Jul 22nd, 2007, 03:44 PM
What happens if you subtract a year from 02/29/2000?

Won't that blow up?

effekt26
Jul 22nd, 2007, 06:03 PM
What happens if you subtract a year from 02/29/2000?

Won't that blow up?

Im pretty sure the inbuilt logic will return a value of 02/28/1999...although i have been known to be wrong before...causing things to blow up :eek:

Crash893
Jul 22nd, 2007, 08:32 PM
.AddYears(-1) is correct. The problem is this line:
date2.AddYears(-1);
What are you doing with the result of this line? It just disappears. ;)



DateTime date2 = DateTime.Now;
this.textBox1.Text = date2.AddYears(-1).ToString("MM/dd/yyyy HH:MM:ss");


that works

rate = rate +1

jmcilhinney
Jul 22nd, 2007, 10:08 PM
Just note that that format is displaying the month in place of the minute. Minutes is lower case "m".

nmadd
Jul 22nd, 2007, 10:59 PM
Im pretty sure the inbuilt logic will return a value of 02/28/1999...although i have been known to be wrong before...causing things to blow up :eek:

You're right. It seems to work just fine. :thumb:

Crash893
Jul 22nd, 2007, 11:13 PM
Just note that that format is displaying the month in place of the minute. Minutes is lower case "m".

as always good catch