Assessing Dates of Birth Using C#


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;

        }

The bit of code in this class is a nested class entitled "AgeBag".  This class is nothing more than a property bag used to contain the years, months, and days a person has been alive coupled with the Booleans indicating whether or not a person's next birthday will occur within the following 30, 60, or 90 days.

        /// <summary>

        /// A property bag for holding the years, months, and days since

        /// between a birth date

        /// </summary>

        public class AgeBag

        {

            public AgeBag()

            {

            }

 

            // member variables

                   private int mYears;

                   private int mMonths;

                   private int mDays;

                   private int mDaysToBirthday;

 

                   private bool mBirthdayIn90Days;

                   private bool mBirthdayIn60Days;

                   private bool mBirthdayIn30Days;

 

            // public properties

            public int AgeYears

            {

                get

                {

                    return mYears;

                }

                set

                {

                    mYears = value;

                }

            }

 

            public int AgeMonths

            {

                get

                {

                    return mMonths;

                }

                set

                {

                    mMonths = value;

                }

            }

 

            public int AgeDays

            {

                get

                {

                    return mDays;

                }

                set

                {

                    mDays = value;

                }

            } 

                             public bool BirthdayIn90Days

                             {

                                      get

                                      {

                                                return mBirthdayIn90Days;

                                      }

                                      set

                                      {

                                                mBirthdayIn90Days = value;

                                      }

                             }

 

                             public bool BirthdayIn60Days

                             {

                                      get

                                      {

                                                return mBirthdayIn60Days;

                                      }

                                      set

                                      {

                                                mBirthdayIn60Days = value;

                                      }

                             }

 

                             public bool BirthdayIn30Days

                             {

                                      get

                                      {

                                                return mBirthdayIn30Days;

                                      }

                                      set

                                      {

                                                mBirthdayIn30Days = value;

                                      }

                             }

 

                             public int DaysToBirthday

                             {

                                      get

                                      {

                                                return mDaysToBirthday;

                                      }

                                      set

                                      {

                                                mDaysToBirthday = value;

                                      }

                             }

        }

    }

}

Code:  frmTestEmailer.cs

This form class is the only class contained in the test application; it provides a simple interface for testing the AgeEvaluator class using a specified birth date coupled with an arbitrary end date (simulating the current date as any date).

The class begins with the default imports. 

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

The next section contains the namespace and class declarations, and default constructor. 

namespace DateAgeCalculation

{

 

    /// <summary>

    /// Demo application used to test the ManageAgeCalcs

    /// class

    /// </summary>

          public partial class Form1 : Form

          {

 

        // default constructor

            public Form1()

            {

                   InitializeComponent();

            }

 

The next bit of code in the application is a method entitled "ShowYourAge"; this method creates and populates an AgeBag class instance based upon the results returned from the AgeEvaluator class GetTimeAlive method.  The method displays a message box containing the results returned in the age bag.  The method is annotated and you can follow what is going on within the method by reading he annotation.


 

        /// <summary>

        /// Method uses AgeEvaluator Class to

        /// determine the age of the subject along with

        /// additional information about the subject's

        /// next birthday

        /// </summary>

            private void ShowYourAge()

            {

                   try

                   {

                // create a new AgeBag to collect the birth and age related

                // information

                       AgeEvaluator.AgeBag ab = new AgeEvaluator.AgeBag();

 

                // get the subject's time alive based upon the

                // date of birth and the end value date

                       ab = AgeEvaluator.GetTimeAlive(dtpBirthDate.Value,

                dtpEndDate.Value);

 

                // display information about the subject's age and date

                // of birth

                       StringBuilder sb = new StringBuilder();

                sb.Append("The amount of time the subject as lived in days,

                months and Years: "

                + Environment.NewLine);

                       sb.Append("Years Alive: " + ab.AgeYears +

                Environment.NewLine);

                       sb.Append("Months Alive: " + ab.AgeMonths +

                Environment.NewLine);

                sb.Append("Days Alive: " + ab.AgeDays + Environment.NewLine

                + Environment.NewLine);

                sb.Append("Pending Birthday: " + Environment.NewLine);

                       sb.Append("Birthday Is Within The Next 90 Days: "

                + ab.BirthdayIn90Days.ToString() + Environment.NewLine);

                sb.Append("Birthday Is Within The Next 60 Days: "

                + ab.BirthdayIn60Days.ToString() + Environment.NewLine);

                sb.Append("Birthday Is Within The Next 30 Days: "

                + ab.BirthdayIn30Days.ToString() + Environment.NewLine

                + Environment.NewLine);

                       sb.Append("Days Until Next Birthday: " +

                ab.DaysToBirthday.ToString()

                + Environment.NewLine);

 

                // display the age and birth day information

                MessageBox.Show("Birth Information" + Environment.NewLine +

                Environment.NewLine + sb.ToString(), "Birthday Test");

                                     

                   }

                   catch (Exception ex)

                   {

                             MessageBox.Show(ex.Message, "Error");

                   }

          }

 

The last bit of code in the form class is the button click event handler used to handle the test button's click event.  The handler merely calls the ShowYourAge method defined in the previous section of this document.

 

        /// <summary>

        /// On click handler used to obtain information

        /// about the subject's age and pending birthday

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

                   private void button1_Click(object sender, EventArgs e)

                   {

                             ShowYourAge();

                    }

 

Summary


The article provide a simple class that may be used to determine the exact age of a person, or to determine the exact amount of time in years, months, and days that has passed between a specific start and end date.  Further, the class will indicate whether or not the person's next birthday will occur within the following 30, 60, or 90 days.

Up Next
    Ebook Download
    View all
    Learn
    View all