Click here to Skip to main content
15,893,722 members
Articles / Web Development / HTML

A Simple Class To Handle Dates

Rate me:
Please Sign up or sign in to vote.
4.78/5 (22 votes)
12 Dec 1999 217.2K   4.4K   57  
A class that simplifies common date handling tasks
<HTML>
<HEAD>
         <TITLE>Documentation for  CSimpleDate Class</Title>

<STYLE TYPE="text/css">
<!--
    LI              {font: 13px Courier New}
    B               {font: 14px Verdana; font-weight: bold}
    A               {font-weight: bold; text-decoration: none}
-->
</STYLE>

</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000040" LINK="#0080FF" ALINK="#0080FF" VLINK="#0080FF">

<FONT SIZE="+2" FACE="Courier New" COLOR="#000000">
<P><Center>Documentation for CSimpleDate C++ Class</Center></p>
<FONT SIZE="-1" FACE="Courier,Courier New" COLOR="#000080">
<p><center>
<UL TYPE="DISC"><LH>Table of Contents</LH></Center>
<li><A HREF="#Constructors">Constructors and Destructors</A><br>
<li><A HREF="#DateMath">Date math routines</A><br>
<li><A HREF="#DataAccess">Data access routines</A>
<li><A HREF="#Operators">Operator assignments conversions equality etc...</A>
<li><A HREF="#Internal">Protected internal class members</A>
<li><A HREF="#Static">STATIC MEMBER FUNCTIONS</A>
<li><A HREF="#NonMember">NON Member data exchange functions</A>
</ul>

<hr>
	<center><A HREF="#Constructors">Constructors and Destructors</A></center><br>
<UL TYPE="CIRCLE">
<LH></LH>
<LI>CSimpleDate(int FormatType=MMDDYYYY);<br>
<LI>	CSimpleDate(LPCSTR DateString,int FormatType=MMDDYYYY);<br>
<LI>	CSimpleDate(long JD,int FormatType=MMDDYYYY);<br>  
<li>virtual ~CSimpleDate();<br>

</UL>
<br>
	<center><A HREF="#DateMath">Date math routines</A></center><br>
<UL TYPE="CIRCLE">
<LH></LH>
<li>	const	CSimpleDate& AddDays(int Days);<br>
<li>	const	CSimpleDate& AddYears(int Yrs);<br>
<li>	const	CSimpleDate& AddMonths(int Mon);<br>
<li>	const	CSimpleDate& SubtractYears(int Yrs);<br>
<li>	const	CSimpleDate& SubtractDays(int Days);
<li>	const	CSimpleDate& SubtractMonths(int Mon);<br>
<li>	virtual int			YearsOld();<br>
</ul>
<br>
	<center><A HREF="#DataAccess">Data access routines</A></center><br>
<UL TYPE="CIRCLE">
<LH></LH>
<li>	LPCSTR		GetFullDateString();<br>
<li>	LPCSTR		GetFullDateStringLong();<br>
<li>	virtual		int GetDayOfWeek();<br>
<li>	virtual		BOOL IsValid();<br>
<li>	long			GetJulianDate(); <br>
<li>	virtual		int	GetDay()<br>
<li>	virtual		int	GetMonth()<br>
<li>	virtual		int	GetYear()<br>
<li>	virtual		void	GetIntegerDate(int& m, int& d,int& y)<br>
<li>	virtual		int		GetHour()<br>
<li>	virtual		int		GetMin()<br>
<li>	virtual		int		GetSeconds()<br>
<li>	virtual		void	GetTimeString(CString& s,BOOL AmPm=TRUE);<br>
<li>	virtual		void	GetTimeString(LPSTR s,int nLen,BOOL AmPm=TRUE);<br>
<li>	virtual		void	GetTimeStringShort(CString& s,BOOL AmPm=TRUE);<br>
<li>	virtual		void	GetTimeStringShort(LPSTR s,int nLen,BOOL AmPm=TRUE);<br>
</ul>
<br>
	<center><A HREF="#Operators">Operator assignments conversions equality etc...</A></center><br>
<UL TYPE="CIRCLE">
<LH></LH>
<li>	operator	LPCSTR();<br>
<li>	operator	long();<br>
<li>	const CSimpleDate& operator = (const CSimpleDate& Date);<br>
<li>	const CSimpleDate& operator = (LPCSTR Date);<br>
<li>	BOOL operator > (const CSimpleDate& Date);<br>
<li>	BOOL operator < (const CSimpleDate& Date);<br>
<li>	BOOL operator >= (const CSimpleDate& Date);<br>
<li>	BOOL operator <= (const CSimpleDate& Date);<br>
<li>	BOOL operator == (const CSimpleDate& Date);<br>
<li>	BOOL operator != (const CSimpleDate& Date);<br>
<li>	BOOL operator > (LPCSTR Date);<br>
<li>	BOOL operator < (LPCSTR Date);<br>
<li>	BOOL operator >= (LPCSTR Date);<br>
<li>	BOOL operator <= (LPCSTR Date);<br>
<li>	BOOL operator == (LPCSTR Date);<br>
<li>	BOOL operator != (LPCSTR Date);<br>
</ul>
<br>
<br>
<br>
		<center><A HREF="#Internal">Protected internal class members</A></center><br>
<UL TYPE="CIRCLE">
<LH></LH>
<li>	virtual	BOOL	SetToday();<br>
<li>	virtual	BOOL	CSimpleDate::ParseDateString(LPCSTR,int& m,int& d,int& y);<br>
<li>	virtual	BOOL	ParseDateString(LPCSTR);<br>
<li>	virtual	long	ConvertToJulian( int month,int day,int year);<br>
<li>	virtual	long	ConvertToJulian();<br>
<li>	virtual	void	ConvertFromJulian(int& Month,int& Day,int& Year);<br>
<li>	virtual 	void	ConvertFromJulian();<br>
<li>	virtual 	void	AdjustDays();<br>
<li>	virtual 	void SetTime();<br>
</ul>
<br>
	<center><A HREF="#Static">STATIC MEMBER FUNCTIONS</A></center><br>
<UL TYPE="CIRCLE">
<LH></LH>
<li>	static	BOOL	VerifyDateFormat(LPCSTR date);<br>
<li>	static	BOOL	FixDateFormat(CString & date);<br>
<li>	static	BOOL	FixDateFormat(LPSTR date);<br>
</ul>
<br>
<br>
<br>
	<center><A HREF="#NonMember">NON Member data exchange functions</A></center><br>
<UL TYPE="CIRCLE">
<LH></LH>
<li>void DDX_SimpleDate(CDataExchange *pDX,int nID,LPSTR str,int nLen);<br>
<li>void DDX_SimpleDate(CDataExchange *pDX,int nID,CString& str);<br>
<li>void DDX_SimpleDate(CDataExchange *pDX,int nID,long& jdate);<br>






<p>
<A HREF="></A><A HREF=" NAME="Constructors"></A>
<HR>
<FONT SIZE="+2" FACE="Courier New" COLOR="#000000">
<Center>Constructors</Center></font>
There are three constructors available. The first one:<br><br>
<B>CSimpleDate(int FormatType=MMDDYYYY)</B>;<br><br>
will initilize the object to todays date. This is the default constructor<br>
Example of use: CSimpleDate D;  or CSimpleDate D(MMDDYY);<p>
The second one:<br><br>
<B>CSimpleDate(LPCSTR DateString,int FormatType=MMDDYYYY)</B>;<br><br>
takes a character string or a CString containing a date properly formatted as 
the first parameter. The optional format type is the second parameter<br>
Example of use: CSimpleDate("12/07/1999"); or CSimpleDate("12/07/99",MMDDYY);<br>
One caveat; The internal representation of the dates are maintaied as a 4 digit year no matter 
what format is in effect. The format only affects output. <p>

The third one:<br><br>
<b>CSimpleDate(long JD,int FormatType=MMDDYYYY)</b>;<br>  <br>
takes a long integer representing a  <A HREF="http://www.capecod.net/~pbaum/date/date0.htm">Julian Date Number</A> 
as a parameter and the optional format type.<br>
<hr><p>


<A HREF="></A><A HREF=" NAME="DateMath"></A>
<FONT SIZE="+2" FACE="Courier New" COLOR="#000000">
<Center>Date Math</Center></font><p>

	<b>const	CSimpleDate& AddDays(int Days);<br>
	const	CSimpleDate& AddYears(int Yrs);<br>
	const	CSimpleDate& AddMonths(int Mon);<br>
	const	CSimpleDate& SubtractYears(int Yrs);<br>
	const	CSimpleDate& SubtractDays(int Days);<br>
	const	CSimpleDate& SubtractMonths(int Mon);<br>
	virtual int			YearsOld();<br></b><p>
These routines are self explanatory. They will take a existing CSimpleDate object and 
perform the requested operation on it. With the exception of <i>YearsOld()</i> they all 
return a reference to self allowing them to be used on the right side of a expression. For 
example look at the following few lines of code.<br><p>

<FONT SIZE="-1" FACE="MS Sans Serif,Arial" COLOR="#8000FF">
CSimpleDate Date("12/12/1990"); &nbsp&nbsp&nbsp&nbsp  // Date=dec 12 1990<br>
CSimpeDate Date2;  &nbsp&nbsp&nbsp&nbsp //Date2 = todays date<br>
Date2=Date.AddMonths(1); &nbsp&nbsp&nbsp&nbsp // date2 now equals Jan 12 1991 and so does Date1.<br>
</FONT><p>

The copy constructor is invoked and assignment can be made in this manner.<br><br>
 The <b>YearsOld()</b>  returns the number of whole years between a CSimpleDate value 
and todays date. Todays  date is the current system date. There is no negative return 0 
will be returned as the smallest value. If the CSimpleDate object is Invalid or if it 
contains a date greater than todays date 0 will be returned.
<hr><p>


<A HREF="></A><A HREF=" NAME="DataAccess"></A>
<FONT SIZE="+2" FACE="Courier New" COLOR="#000000">
<Center>Class Access Functions</Center></font><p>

	<B>LPCSTR		GetFullDateString();</B><br><br>
returns a string containing the date in the format of "Sun Dec 7 1941"<br><br>
	<b>LPCSTR		GetFullDateStringLong();</b><br><br>
returns a string containing the date in the format of "Sunday December 7 1941"<br><br>
	<b>int 		GetDayOfWeek();</b><br><br>
returns integer representing the day of the week where Sunday=1 Moday=2 etc;<br><br>
	<b>BOOL IsValid();<br></b><br>
returns TRUE if the CSimpleDate object conatins a vaild date else FALSE;<br><br>
	<b>long			GetJulianDate(); <br></b><br>
returns a long int representing the Julian date number for this CSimpeDate objects 
Gregorian date<br><br>
<b>	int	GetDay()<br>
	int	GetMonth()<br>
	int	GetYear()<br>
	void	GetIntegerDate(int& m, int& d,int& y)<br></b><br>
These all return integers representing the internal values held for the requested objects<br><br>

<br>
<b>	int		GetHour()<br>
	int		GetMin()<br>
	int		GetSeconds()<br></b><br>
These all return integers representing the internal values held for the requested objects. 
The GetHour function returns are based on a 24 hour clock and are not adjusted 
for AM PM<br><br>
	<b>void	GetTimeString(CString& s,BOOL AmPm=TRUE);<br></b>
	<b>void	GetTimeString(LPSTR s,int nLen,BOOL AmPm=TRUE);<br></b><br>
returns a string representing the current system time. The hour portion is adjusted for a 12 
hour clock. If AmPm is true ( default value) AM or PM will be appended to the string. The 
returned string will be in the format of "9:45:55 PM"<br><br>
<b>	void	GetTimeStringShort(CString& s,BOOL AmPm=TRUE);<br>
	void	GetTimeStringShort(LPSTR s,int nLen,BOOL AmPm=TRUE);<br></b><br>
returns a string representing the current system time. The hour portion is adjusted for a 12 
hour clock. If AmPm is true ( default value) AM or PM will be appended to the string. The 
returned string will be in the format of "9:45 PM"<br>

<hr><p>


<A HREF="></A><A HREF=" NAME="Operators"></A>
<FONT SIZE="+2" FACE="Courier New" COLOR="#000000">
<Center>Operators and comparision functions</Center></font><p>

	<b>operator	LPCSTR();<br></b>
returns a string representation of the CSimpledate object. This also allows for a CSimpleDate to be constructed 
with a CSimpleDate as a parameter.<br><p>
<FONT SIZE="-1" FACE="MS Sans Serif,Arial" COLOR="#8000FF">
CSimpleDate Date("12/09/1946");<br>
CString s=(LPCSTR)Date;<br>
CSimpleDate S2(Date); // no such constructor but will work because it converts the Date to a LPCSTR <br>
VERIFY(s=="12/09/1946");<br><br>
</FONT>
	<b>operator	long();<br></B>
returns a long representing the Julian Date number for this CSimpleDate object<br><p>
<FONT SIZE="-1" FACE="MS Sans Serif,Arial" COLOR="#8000FF">
CSimpleDate Date("12/09/1946");<br>
long s=(long)Date;<br>
VERIFY(s==Date.GetJulianDate());<br><br>
</FONT>
	<b>const CSimpleDate& operator = (const CSimpleDate& Date);<br>
	const CSimpleDate& operator = (LPCSTR Date);<br></B>
Copy constructors basic stuff nothing fancy <br><br>
	<b>BOOL operator > (const CSimpleDate& Date);<br>
	BOOL operator < (const CSimpleDate& Date);<br>
	BOOL operator >= (const CSimpleDate& Date);<br>
	BOOL operator <= (const CSimpleDate& Date);<br>
	BOOL operator == (const CSimpleDate& Date);<br>
	BOOL operator != (const CSimpleDate& Date);<br>
	BOOL operator > (LPCSTR Date);<br>
	BOOL operator < (LPCSTR Date);<br>
	BOOL operator >= (LPCSTR Date);<br>
	BOOL operator <= (LPCSTR Date);<br>
	BOOL operator == (LPCSTR Date);<br>
	BOOL operator != (LPCSTR Date);<br></b><br>
These are all overloaded equality operators and are used in the normal manner. They can be 
applied between CSimpleDate objects and/or string representations of dates ( rvalues only). For example:<br><p>
<FONT SIZE="-1" FACE="MS Sans Serif,Arial" COLOR="#8000FF">
CSimpleDate Date("12/09/1998");<br>
CSimpeDate Date2;<br>
BOOL res=Date2 > Date<br>
res=Date > "12/01/1998";<br>
if( Date2 > Date1) <br>
{<br>
&nbsp&nbsp&nbsp&nbsp DoThis();<br>
}<br>
else if(Date < "12/12/1998")<br>
&nbsp&nbsp&nbsp&nbsp DoThis();<br>
else<br>
&nbsp&nbsp&nbsp&nbsp ForGetIt();<br>

</FONT>

<hr><p>



<A HREF="></A><A HREF=" NAME="Internal"></A>
<FONT SIZE="+2" FACE="Courier New" COLOR="#000000">
<Center>Internal Protected  functions</Center></font><p>

	<b>BOOL	SetToday();<br>
	BOOL	CSimpleDate::ParseDateString(LPCSTR,int& m,int& d,int& y);<br>
	BOOL	ParseDateString(LPCSTR);<br>
	long	ConvertToJulian( int month,int day,int year);<br>
	long	ConvertToJulian();<br>
	void	ConvertFromJulian(int& Month,int& Day,int& Year);<br>
	void	ConvertFromJulian();<br>
	void	AdjustDays();<br>
	void SetTime();<br></b><br>
These are all used in setting and maintaining the class data. All functions are virtual so they can be 
overridden in inherited classes. The names are pretty explanatory.Please examine the source code as 
to the functionality for each member.<br>
<hr><p>


<A HREF="></A><A HREF=" NAME="Static"></A>
<FONT SIZE="+2" FACE="Courier New" COLOR="#000000">
<Center>Static functions</Center></font><p>

	<b>BOOL	VerifyDateFormat(LPCSTR date);<br></b><br>
Passed a string representation of a date I.E "12/01/90" or "12-08-1998" will determine if the date is 
correct and formatted in a manner that the Class can recognize. Invalid dates I.E "02/30/1998" will 
also be caught. Returns true if OK else false<br><br>
	<b>BOOL	FixDateFormat(CString & date);<br></b>
	<b>BOOL	FixDateFormat(LPSTR date);<br></b><br>
This pair of functions will attempt to format a unformatted string. An unformatted string is one that 
contains no date seperators such as "121290" or "12121997". If succesful the string will be formatted 
and checked for validity. The LPSTR version<B> ASSUMES</b> that the string is long enough to hold the 
formatted date.<br><br>
<hr><p>


<A HREF="></A><A HREF=" NAME="NonMember"></A>
<FONT SIZE="+2" FACE="Courier New" COLOR="#000000">
<Center>Non Member functions</Center></font><p>

		<b>DDX_SimpleDate(CDataExchange *pDX,int nID,LPSTR str,int nLen);<br>
		DDX_SimpleDate(CDataExchange *pDX,int nID,CString& str);<br>
		DDX_SimpleDate(CDataExchange *pDX,int nID,long& jdate);<br></b><br>
These are data exchange functions that can be used to input and display CSimpleDate type dates. The 
only data that needs to be saved to and from a database or datafile is either a string representation 
of the date or the Julian Date number. By saving a Julian Date number simple comparisions can be rapidly 
made between date fields I.E. greater than less than equal to. These functions also provide error checking 
to assure that a valid date is entered. They are used exactly like the DDX_xxx functions provided by MFC. 
For example: <br><p>
<FONT SIZE="-1" FACE="MS Sans Serif,Arial" COLOR="#8000FF">
void CMainView::DoDataExchange(CDataExchange* pDX)<br>
{<br>
&nbsp&nbsp&nbsp&nbsp	CFormView::DoDataExchange(pDX);<br>
&nbsp&nbsp&nbsp&nbsp	//{{AFX_DATA_MAP(CMainView)<br>
&nbsp&nbsp&nbsp&nbsp	DDX_SimpleDate(pDX, IDC_THEDATE, m_TheDate);	// string or cstring <br>
&nbsp&nbsp&nbsp&nbsp	DDX_SimpleDate(pDX, IDC_THEDATE2, m_Date2);		// long <br>
&nbsp&nbsp&nbsp&nbsp	DDX_Text(pDX, IDC_RESULTS, m_R1);<br>
&nbsp&nbsp&nbsp&nbsp	//}}AFX_DATA_MAP<br>
}<br>

</FONT>

<hr><p>


</BODY>
</HTML>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


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

Comments and Discussions