Thread: CDate Class - handle date manipulation simply

  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.

  2. #2
    Registered User
    Join Date
    Jun 2003
    Posts
    245
    Nice useful class, thanks.

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Boost has a date/time class now.
    For future reference date/time classes are easier to build if you do calculations based upon the julian date. This makes things easy for both dates in the Julian calender and dates in the Gregorian calender.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Originally posted by Stoned_Coder
    For future reference date/time classes are easier to build if you do calculations based upon the julian date. This makes things easy for both dates in the Julian calender and dates in the Gregorian calender.
    :sigh:
    Dates are so ridiculously complex... to think, we could fix all that by simply shortening the metre a bit.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  5. #5
    Registered User
    Join Date
    Jul 2003
    Posts
    9

    date header file

    Where do you put the header file in your C++ directory?

    This looks like it would help tons with the new project I am working on.

  6. #6
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    _Elixia_: Thanks.

    TheShaggmeister: You can put the header anywhere you would normally put a header. The simplest place is in the same directory as your program source or in a subdirectory of it. I personally have a special directory where I put my libraries and just point my compilers at it.

    Stoned_coder: While it's true that it would be easier for manipulating dates, my original intent was to create a class similar to the way dates work in the language I use at work. Thanks for the tip, in any case.

    If anyone is curious, the way it works is by storing the date as the number of days after Dec 31 of the year 1 BC. So, for example, the date Jan 1, 0000 would be stored as "1".
    Last edited by LuckY; 07-16-2003 at 08:39 AM.

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