CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2006
    Posts
    28

    Add/subtract current date

    Hi,

    Can any one tell me the way to do add or subtract on the current date.

    Suppose, todays date is 09 April, 2010. Now, I want to subtract 6 months from it.

    Any help will be highly appreciated.

    Regards.

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Add/subtract current date

    Take a look at CTime and CTimeSpan.

  3. #3
    Join Date
    Jan 2006
    Posts
    28

    Re: Add/subtract current date

    Thanks for the reply skizmo.

    CTimeSpan requires days in its constructor. How do I convert number of months into days.

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Add/subtract current date

    Quote Originally Posted by salman83 View Post
    Thanks for the reply skizmo.

    CTimeSpan requires days in its constructor. How do I convert number of months into days.
    It's not very hard to get the current month and figure out what six months before that is. Get the current month and subtract six. If you end up with a negative number, add 12 and subtract 1 from the year. Whichever way you go, you'll have to figure out what to do if you end up with an invalid date.

  5. #5
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Add/subtract current date

    It depends what you expect...

    months don't have a fixed number of days, so do you can't just do months*30 or months*31 to get the number of days.

    Do you just want to handle month handling ? if so, what do you expect to get when you go back 6 months from August 31 ? the 31st of February doesn't exist, do you want it to go to the last day of february whch could be 28 or 29th. or do you want it to adjust to the 3rd or 4th of march ?

    Handling something like this will take some effort on your part.
    The easiest in cases like this is to usually take the date as days/months/year (CTime) subtract months, do clipping if months turns negative by adding 12 and subtracting a year. then adjust the day portion to make it valid.

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

Featured