[RESOLVED] subtract a year from datetime.now
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?
Re: subtract a year from datetime.now
.AddYears(-1) is correct. The problem is this line:
Code:
date2.AddYears(-1);
What are you doing with the result of this line? It just disappears. ;)
Code:
DateTime date2 = DateTime.Now;
this.textBox1.Text = date2.AddYears(-1).ToString("MM/dd/yyyy HH:MM:ss");
Re: subtract a year from datetime.now
What happens if you subtract a year from 02/29/2000?
Won't that blow up?
Re: subtract a year from datetime.now
Quote:
Originally Posted by szlamany
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:
Re: subtract a year from datetime.now
Quote:
Originally Posted by nmadd
.AddYears(-1) is correct. The problem is this line:
Code:
date2.AddYears(-1);
What are you doing with the result of this line? It just disappears. ;)
Code:
DateTime date2 = DateTime.Now;
this.textBox1.Text = date2.AddYears(-1).ToString("MM/dd/yyyy HH:MM:ss");
that works
rate = rate +1
Re: subtract a year from datetime.now
Just note that that format is displaying the month in place of the minute. Minutes is lower case "m".
Re: subtract a year from datetime.now
Quote:
Originally Posted by effekt26
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:
Re: subtract a year from datetime.now
Quote:
Originally Posted by jmcilhinney
Just note that that format is displaying the month in place of the minute. Minutes is lower case "m".
as always good catch