13,772,369 members
Tip/Trick
alternative version

#### Stats

3.7K views
5 bookmarked
Posted 30 Nov 2016
Licenced CPOL

# Calendar Days Between Date/Time Pairs

, 30 Nov 2016
Displays a date/time difference as calendar days or 24-hour days and can show hours or minutes for the first 24 hours

## Introduction

How long has it been since your last antivirus scan, e-mail login or other recurring process? The answer is usually displayed as minutes, hours or days. Are these days calendar days or 24-hour days? If the times are available, you can figure that out. After the first 24 hours, I presume that most people would like to see a difference as calendar days. The following solution shows how to calculate calendar or 24-hour days.

## Solution

The following function allows for displaying a time difference as minutes, hours or days, where you can choose between calendar days and 24-hour days. In this function and the sample program `CalDayDiff`, the `MFC` class `CTime` is used. Starting with Visual Studio 2005, the upper date limit on `CTime` became December 31, 3000. Prior to that version, the upper date limit on `CTime` was January 18, 2038. This program also contains source code for using the `MFC` class `COleDateTime`, which has the upper date limit of December 31, 9999.

```enum SpanType {Minutes, Hours, Days};

// Action:	Calculates difference between two CTime objects as minutes, hours or days.
//			The first two units apply to the first 24 hours when b24Show is true.
//			The last unit can either be 24-hour days or calendar days.
int CCalDayDiffDlg::GetDaysDiff (CTime& dtFirst, CTime& dtSecond, bool b24Show /* = false */,
bool bCalDays /* = true */, int* pnType /* = nullptr */)
{
CTimeSpan dtSpan;

if (b24Show) {
dtSpan = dtSecond - dtFirst;

if (dtSpan.GetDays() == 0) {
int nHours = dtSpan.GetHours();
if (nHours == 0) {
if (pnType != nullptr)
*pnType = Minutes;
return dtSpan.GetMinutes();
}
else {
if (pnType != nullptr)
*pnType = Hours;
return nHours;
}
}
}

if (bCalDays) {		// calendar days
CTime dtFirstDate (dtFirst.GetYear(), dtFirst.GetMonth(), dtFirst.GetDay(), 0, 0, 0);
CTime dtSecondDate (dtSecond.GetYear(), dtSecond.GetMonth(), dtSecond.GetDay(), 0, 0, 0);
dtSpan = dtSecondDate - dtFirstDate;
}
else {				// 24-hour days
dtSpan = dtSecond - dtFirst;
}

if (pnType != nullptr)
*pnType = Days;
return static_cast<int> (dtSpan.GetDays());
}	// GetDaysDiff```

If requested, this function handles the first 24 hours differently by returning the number of minutes or hours between the input date/time objects. After that, if determining calendar days is selected, then `CTime` objects are created that have their time components set to zero. From these objects, a time-span object is calculated. Otherwise, this time-span object is calculated from the input time/date objects. From this object, the number of elapsed days is returned.

## Sample Program

The program `CalDayDiff` can show time differences with various options. First, alter the dates and/or times to produce a time difference. Then choose between calendar days and 24-hour days and whether minutes or hours should be displayed for the first 24 hours. These choices can be changed to see the effects of different choices. The data/time difference will be updated with each change. See the above images for some sample choices.

This program contains the following macro:

`#define SINGULAR(n) (abs (n) == 1 ? _T("") : _T("s"))`

It is essentially the macro that I wrote about in Singular vs. Plural in Item Counts. The difference is that it includes `abs` since `n` might be negative. Its purpose is to make it easier to select between singular and plural versions of words. It is first used in the following line of `CalDayDiff`:

`strMsg.Format (_T("%d minute%s"), nResult, SINGULAR (nResult));`

## Share

 United States
While working as an engineer, I occasionally applied my problem-solving skills to software problems. For example, I noticed that numbers in increasing filenames did not always increase. I tinkered with solutions and subsequently wrote an article that was published in Dr. Dobb’s Journal.

After that job fizzled, I switched to programming full time and have applied my programming skills to multimedia education, web-based games of skill, legal-compliance and ethics-training courses, and shareware programs.