Thread: CDate Class - handle date manipulation simply

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856

    CDate Class - handle date manipulation simply

    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
    Last edited by LuckY; 07-15-2003 at 04:22 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Advancing day by day until it matches a second date
    By nhubred in forum C++ Programming
    Replies: 1
    Last Post: 05-30-2009, 08:55 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. Getting other processes class names
    By Hawkin in forum Windows Programming
    Replies: 3
    Last Post: 03-20-2008, 04:02 PM
  4. date class
    By aquaman in forum C++ Programming
    Replies: 3
    Last Post: 10-10-2001, 09:57 AM
  5. date class
    By aquaman in forum C++ Programming
    Replies: 6
    Last Post: 09-16-2001, 07:29 PM