|
-
Jul 22nd, 2007, 03:12 PM
#1
Thread Starter
Fanatic Member
[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?
-
Jul 22nd, 2007, 03:27 PM
#2
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");
-
Jul 22nd, 2007, 03:44 PM
#3
Re: subtract a year from datetime.now
What happens if you subtract a year from 02/29/2000?
Won't that blow up?
-
Jul 22nd, 2007, 06:03 PM
#4
Addicted Member
Re: subtract a year from datetime.now
 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
-
Jul 22nd, 2007, 08:32 PM
#5
Thread Starter
Fanatic Member
Re: subtract a year from datetime.now
 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
-
Jul 22nd, 2007, 10:08 PM
#6
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".
-
Jul 22nd, 2007, 10:59 PM
#7
Re: subtract a year from datetime.now
 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 
You're right. It seems to work just fine.
-
Jul 22nd, 2007, 11:13 PM
#8
Thread Starter
Fanatic Member
Re: subtract a year from datetime.now
 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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|