Results 1 to 8 of 8

Thread: [RESOLVED] subtract a year from datetime.now

  1. #1

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Resolved [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?

  2. #2
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    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");

  3. #3
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: subtract a year from datetime.now

    What happens if you subtract a year from 02/29/2000?

    Won't that blow up?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  4. #4
    Addicted Member effekt26's Avatar
    Join Date
    Nov 2006
    Posts
    138

    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

  5. #5

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    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

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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".
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    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
    You're right. It seems to work just fine.

  8. #8

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width