65.9K
CodeProject is changing. Read more.
Home

MyDate - A System Independent Date Object

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.85/5 (9 votes)

Sep 15, 2003

BSD

3 min read

viewsIcon

83761

downloadIcon

1302

MyDate is a comprehensive class that wraps the date data type and provides many useful functions and operators for dealing with dates.

Introduction

C++ has built in types to deal with basic information like integers, boolean, etc. When it comes to dealing with dates, you probably have to switch to the classes provided with MFC, Qt or some other libraries. MyDate is a comprehensive class that wraps the date data type and provides many useful functions and operators for dealing with dates. It is really simple to use and is 100% cross-platform.

Background

Basic knowledge of classes and objects.

Using the Code

Before including mydate.h in your source code, you should include string.h using the following #include statement:

#include <string>
using namespace std;
#include "mydate.h"

MyDate objects can be created using the default constructor, or with the overloaded constructor which takes a date in the DD/MM/YYYY format as its argument. A MyDate object created using the default constructor will contain the system date. Later, date can be changed by calling the setDate(const char*) member function which takes a string in the DD/MM/YYYY format as argument. You can also assign a new date using the = operator. The argument can be either a string in the DD/MM/YYYY format or another MyDate object. Please keep in mind that class MyDate and its relative exception class MyDateException are both part of a namespace called openutils.

// different ways for creating a MyDate object and assigning it a value
openutils::MyDate date1; // system date
openutils::MyDate date2("07/09/2003");
openutils::MyDate date3 = date2;
openutils::MyDate date4 = "19/12/2002";
date2.setDate("01/01/2003");

When you create a date object, you can also specify date in the DD/MM/YY format. If you say "01/01/03", then the year will be automatically converted to "2003". But if you have said "01/01/3", then the year will be treated as 3 AD (CE). MyDate does not deal with years before our common era (BC or BCE).

The cream of MyDate class is the way in which it allows you to format and retrieve dates. For this, you call the getDate() member function which returns a string that contains the date in the default DD/MM/YYYY format:

string s = date3.getDate();
printf("%s",s.c_str()); // this will print: 07/09/2003

There is an overloaded getDate(const char*) function, that takes a string as argument and replaces some special sequences in this string with the appropriate day, month or year values. Please see the following table for a description of all formatting sequences:

Format Output
DD Day as two digits (e.g. 01)
MM Month as two digits (e.g. 12)
MON Month as three characters (e.g. DEC)
MONTH Month as full string (e.g. December)
YY Year as two digits (e.g. 03)
YYYY Year as four digits (e.g. 2003)

Please keep in mind that formatting sequences are case sensitive. Now let us look at an example of formatting date the way you like. Say, you want to display date in the following format:

Current system date is 11, September 2003 

The following code will do just that:

char buff[51];
strcpy(buff,date1.getDate("Current system date is DD,MONTH YYYY").c_str());
printf("%s",buff);

To add days, months or years, call the addDays(int), addMonths(int) or addYears(int) member functions. The following code will add 10 days to the system date:

MyDate sys_date;
sys_date.addDays(10);

In the same way, you can deduct days, months or years from a MyDate object by calling lessDays(int days), lessMonths(int months) and lessYears(int years) functions.

The difference between two MyDate objects can be expressed as either differenceInDays(MyDate), differenceInMonths(MyDate) or differenceInYears(MyDate).

MyDate d1("01/05/2003");
MyDate d2("10/05/2003");
int diff = d2.differenceInDays(d1); // diff = 9

For comparing two MyDate objects, you can use the following overloaded operators:

<,>,==,!=

while(date1 != date2) {
date2.addDays(1);
}

Notes

All functions that affect the value of the date object will throw the openutils::MyDateException. So all MyDate related code should be enclosed in a try-catch block like:

try {
// ....
}catch(openutils::MyDateException* ex) {
printf("Error: %s",ex->getMessage().c_str());
}

Some other useful functions in the MyDate class:

  1. setDay(int day) - sets the day of the date object to day.
  2. setMonth(int month) - sets the month of the date object to month.
  3. setYear(int year) - sets the month of the date object to year.
  4. All the above setXXX functions have their corresponding getXXX functions that return an integer value.
  5. getShortMonth() - returns month in the format "MON". (Sep, Feb, etc.)
  6. getFullMonth() - returns full month name.
  7. isLeapYear() - returns true if leap year.

History

  • Created: 28th August, 2003
  • First update: 11th September, 2003
  • Second update: 20th September, 2003
  • Third update: 10th September, 2009