Click here to Skip to main content
15,896,606 members
Articles / Programming Languages / C++

MyDate - A System Independent Date Object

Rate me:
Please Sign up or sign in to vote.
3.85/5 (11 votes)
10 Sep 2009BSD3 min read 82.4K   1.3K   25   15
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:

C++
#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.

C++
// 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:

C++
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:

FormatOutput
DDDay as two digits (e.g. 01)
MMMonth as two digits (e.g. 12)
MONMonth as three characters (e.g. DEC)
MONTHMonth as full string (e.g. December)
YYYear as two digits (e.g. 03)
YYYYYear 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:

C++
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:

C++
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).

C++
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:

<,>,==,!=

C++
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:

C++
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

License

This article, along with any associated source code and files, is licensed under The BSD License


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalnot so full Pin
Serge Savostin10-Sep-09 4:41
Serge Savostin10-Sep-09 4:41 
GeneralRe: not so full Pin
AnOldGreenHorn10-Sep-09 5:59
AnOldGreenHorn10-Sep-09 5:59 
Please see my response to "Problem in lessDay".
GeneralProblem with lessDay Pin
mayo20008-Sep-09 4:57
mayo20008-Sep-09 4:57 
GeneralRe: Problem with lessDay Pin
AnOldGreenHorn9-Sep-09 19:35
AnOldGreenHorn9-Sep-09 19:35 
GeneralAuthors contact indo Pin
Sharad Kelkar10-Feb-09 4:32
Sharad Kelkar10-Feb-09 4:32 
Generalthere is small problem in the program that should be correct Pin
farhanx8-Jun-06 0:36
farhanx8-Jun-06 0:36 
GeneralRe: there is small problem in the program that should be correct Pin
farhanx8-Jun-06 0:38
farhanx8-Jun-06 0:38 
QuestionHow about time? Pin
Jennifer James13-Jul-05 14:59
sussJennifer James13-Jul-05 14:59 
AnswerRe: How about time? Pin
AnOldGreenHorn13-Jul-05 17:36
AnOldGreenHorn13-Jul-05 17:36 
GeneralIs this class Thread-Safe Pin
Mash23-Sep-04 18:44
Mash23-Sep-04 18:44 
GeneralRe: Is this class Thread-Safe Pin
AnOldGreenHorn27-Sep-04 18:10
AnOldGreenHorn27-Sep-04 18:10 
QuestionAny date? Pin
FlyingDancer8-Dec-03 14:56
FlyingDancer8-Dec-03 14:56 
AnswerRe: Any date? Pin
AnOldGreenHorn7-Jul-04 3:11
AnOldGreenHorn7-Jul-04 3:11 
Generalboost datetime Pin
Jonathan de Halleux15-Sep-03 0:39
Jonathan de Halleux15-Sep-03 0:39 
GeneralRe: boost datetime Pin
vijay mathew15-Sep-03 5:30
vijay mathew15-Sep-03 5:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.