Hello,
I just noticed a few people having problems with dates and thought someone might find a class I wrote to be useful. It is called CDate. It can:
- automatically retrieve the current system date
- set the date using numeric month, day, year
- set the date using a user defined string format
- increment/decrement a date by any number of days
- compare dates with the relational operators (<=>, ==, !=)
- find the difference between two dates
- format a date into a user-defined string (including usage of the month names in user-defined case and length)
- do all the above with complete accuracy taking in leap years, etc
Download it and let me know what you think. It's in the attached zip.
Here are some examples:
Code:
CDate date; // retrieves current date
date += 28; // set date to 28 Days Later
const char *today = "20030715";
if (date != CDate(today)) {
cout << date.Format("Mmmmmmmmm DD, YYYY") << " is not today!\n";
cout << "There are " << (date - CDate(today)) << " days between " <<
date.Format("MM/DD/YY") << " and " << CDate(today).Format("mm/dd/yy\n");
}
cout << "\nToday is: " << (const char*)CDate() << endl;
cout << "Or more nicely put, today is: " << CDate().Format("Mmm DD, yyyy\n");
That will output this:
August 12, 2003 is not today!
There are 28 days between 08/12/03 and 07/15/03
Today is: 20030715
Or more nicely put, today is: Jul 15, 2003