Introduction
This article describes an approach to assessing the difference between a specified beginning and end date. The example was written in the context of comparing a birth date to a specific end date but the same approach could be used to calculate the number of years, months, and days between a specified start and end date.

Figure 1: Test Application Main Form
Getting Started
In order to get started, unzip the included project and open the solution in the Visual Studio 2008 environment. In the solution explorer, you should note these files (Figure 2):

Figure 2: Solution Explorer
The solution contains a single project called DateAgeCalculation. This project contains a class entitled AgeEvaluator that is used to perform the date related calculations and a single form class used to test the AgeEvalator class. The form contains to date picker controls which are used to set the date of birth and the end date (used to simulate today as any day of the year).
Code: AgeEvaluator.cs
The AgeEvaluator class contains a couple of methods used to calculate either the total number of years the subject has been alive, or used to calculate the number of years, months, and days the subject has been alive. Further, the class determines whether or not the subject's birthday will occur within 30, 60, or 90 days of the specified end date.
The class begins with the default imports:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
The next section contains the namespace and class declarations. There is no specified default constructor for the class as both contained methods are static.
namespace DateAgeCalculation
{
public class AgeEvaluator
{
The first method contained in this class is used to calculate the person's age in years only. This method accepts the person's date of birth as an argument and all calculations are based upon comparing the date of birth to the current date. The return type is specified as a nullable integer so that, if the method is passed a birthday that is greater than the current date, the method may return a null. The code in this method is annotated and you can follow what the method does by reading that annotation.
/// <summary>
/// Returns the years alive only based upon
/// the date of birth
/// </summary>
/// <param name="birthDay"></param>
/// <returns>int?</returns>
public static int? GetAgeInYears(DateTime birthDay)
{
//return null if the date of birth
//greater than the current date
if (birthDay > DateTime.Now)
return null;
// get the basic number of years
int years = DateTime.Now.Year - birthDay.Year;
// adjust the years against this year's
// birthday
if (DateTime.Now.Month < birthDay.Month ||
(DateTime.Now.Month == birthDay.Month &&
DateTime.Now.Day < birthDay.Day))
{
years--;
}
// Don't return a negative number
// for years alive
if (years >= 0)
return years;
else
return 0;
}
That next method contained in the class is used to calculate the time a person has been alive in years, months, and days. The method returns an AgeBag class instance; this class will be described in the next section of this document but in general, it is a property bag used to hold the number of years, months, and days a person has been alive coupled with three Boolean values used to determine whether or not a person's next birthday will fall within 30, 60, or 90 days of the end date specified in the methods arguments. This method is annotated and you may read what each section of the code does by following the annotation
/// <summary>
/// Get the years, months, and days alive between
/// the date of birth and a specified end date;
/// use current date for the end date in normal
/// calculations
/// </summary>
/// <param name="birthDate"></param>
/// <param name="endDate"></param>
/// <returns></returns>
public static AgeBag GetTimeAlive(DateTime birthDate, DateTime
endDate)
{
if (endDate < birthDate)
{
System.Windows.Forms.MessageBox.Show("Invalid end date", "Error");
return new AgeBag();
}
int years = endDate.Year - birthDate.Year;
int months = endDate.Month - birthDate.Month;
int days = endDate.Day - birthDate.Day;
// use the original days value
// to adjust the month where the
// day has passed
if (days < 0)
months -= 1;
// adjust month and years where
// month has passed
while (months < 0)
{
months += 12;
years -= 1;
}
// adjust days for the current year
TimeSpan timeSpan = endDate - birthDate.AddYears(years).AddMonths(months);
// dispose of fractional portion of total days
days = Convert.ToInt32(Math.Round(timeSpan.TotalDays));
// create and populate an instance of
// the age bag class to keep the values
// calculated for the birth date
AgeBag ab = new AgeBag();
ab.AgeDays = days;
ab.AgeMonths = months;
// get rid of negative number of years
if (years >= 1)
ab.AgeYears = years;
else
ab.AgeYears = 0;
// get the timespan between the date of birth and end date
DateTime dtThisBirthday = new DateTime(DateTime.Now.Year,birthDate.Month, birthDate.Day);
TimeSpan ts = dtThisBirthday - endDate;
// round off the fractional days portion and set
// the agebag property used to hold the days remaining
// before the next birthday
ab.DaysToBirthday = (int)Math.Round(ts.TotalDays);
// if the days until the next birthday in
// a negative number (already passed), recalculate the days
// until the next birthday using the future birthday
if (ab.DaysToBirthday < 0)
{
DateTime nextBirthday = new DateTime(endDate.Year + 1,birthDate.Month, birthDate.Day);
TimeSpan tsNext = nextBirthday - endDate;
ab.DaysToBirthday = Convert.ToInt32(Math.Round(tsNext.TotalDays));
}
// determine whether or not the subject's next
// birthday is between 61 and 90 days away
if (ab.DaysToBirthday <= 90 && ab.DaysToBirthday > 60)
ab.BirthdayIn90Days = true;
else
ab.BirthdayIn90Days = false;
// determine whether or not the subject's next
// birthday is between 60 and 31 days
if (ab.DaysToBirthday <= 60 && ab.DaysToBirthday > 30)
ab.BirthdayIn60Days = true;
else
ab.BirthdayIn60Days = false;
// determine whether or not the subject's next
// birthday will fall within the next 30 days
if (ab.DaysToBirthday <= 30 && ab.DaysToBirthday >= 0)
ab.BirthdayIn30Days = true;
else
ab.BirthdayIn30Days = false;
// return the populated airbag class instance
// to the caller
return ab;
}
Join the conversation! Your thoughts help the community grow.