Date Addition
This could very well be a dumb question, but if I have a date stored in a variable e.g.
beginDate = 12/31/02
if I add 90 to the date will it give me a date 90 days from the
beginning date?
EndDate = beginDate + 90;
"stored in a variable" ... err, what type of variable? If it''s a string, no.
:::: Lektrix ::::
[ Google || ACCU || BarrysWorld || E-Mail Me ]
:::: Lektrix ::::
[ Google || ACCU || BarrysWorld || E-Mail Me ]
[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || [email=lektrix@barrysworld.com]E-Mail Me[/email] ]
Ok.. that makes sense.. now is there a DATE variable type that you can add/subtract from or do I have to jump through hoops to find the difference in days between two dates?
Use could use 3 variables i.e. :
int day = 20;
int month = 12;
int year = 03;
that way u could add to days. If you want could wrap those variables up in a class, that way u could have multiple dates. You could then output to the screen using
cout << day << "\" << month << "\" << year;
You would have to write your own functions to detect adding and subtraction - say u add 90 days, u would then detect if the number is greater than 30, if it is then u need to increase the month by one like month++. u may also want to test the year- if the days is greater than 30, and the month is 12, then u would do month++; This is what u could do to implement your own date functions. If ur using win32 then there are functions.
int day = 20;
int month = 12;
int year = 03;
that way u could add to days. If you want could wrap those variables up in a class, that way u could have multiple dates. You could then output to the screen using
cout << day << "\" << month << "\" << year;
You would have to write your own functions to detect adding and subtraction - say u add 90 days, u would then detect if the number is greater than 30, if it is then u need to increase the month by one like month++. u may also want to test the year- if the days is greater than 30, and the month is 12, then u would do month++; This is what u could do to implement your own date functions. If ur using win32 then there are functions.
I think there's a date variable type only in MFC, if you're
not using MFC you'll have to create a struct/class like this:
You may include your own functions (i.e. "SetDay"), and you must
create an array with the number of days for each Month, the code
I gave you is assuming that every month has 30 days...
Hope that helps...
[edit] Fixed a little bug in the source code...
KaMiKaZe
[edited by - Kamikaze15 on January 12, 2003 4:17:44 PM]
not using MFC you'll have to create a struct/class like this:
class DATE{public: void SetDate(char Day, char Month, UINT Year){pDay=Day;pMonth=Month;pYear=Year;p_fNormalizeValues();} void AddDays(char AddValue){pDay+=AddValue;p_fNormalizeValues();} void AddMonths(char AddValue){pMonth+=AddValue;p_fNormalizeValues();} void AddYear( UINT AddValue ){pYear+=AddValue;} char GetDay(){return pDay;} char GetMonth(){return pMonth;} UINT GetYear(){return pYear;}private: void p_fNormalizeValues(); char pDay; char pMonth; UINT pYear;};void DATE::p_fNormalizeValues(){ while(pDay>30) { pDay-=30; pMonth++; } while(pDay<1) { pDay+=30; pMonth--; } while(pMonth>12) { pMonth-=12; pYear++; } while(pMonth<1) { pMonth+=12; pYear--; }}DATE MyDateVariable;MyDateVariable.SetDate( 01, 01, 2003 );
You may include your own functions (i.e. "SetDay"), and you must
create an array with the number of days for each Month, the code
I gave you is assuming that every month has 30 days...
Hope that helps...
[edit] Fixed a little bug in the source code...
KaMiKaZe
[edited by - Kamikaze15 on January 12, 2003 4:17:44 PM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement