|
-
April 9th, 2010, 06:15 AM
#1
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.
-
April 9th, 2010, 06:28 AM
#2
Re: Add/subtract current date
Take a look at CTime and CTimeSpan.
-
April 9th, 2010, 06:46 AM
#3
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.
-
April 9th, 2010, 07:34 AM
#4
Re: Add/subtract current date
 Originally Posted by salman83
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.
-
April 9th, 2010, 08:02 AM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|