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.
![date diff in C#]()
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):
![date diff in C#]()
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:
The next section contains the namespace and class declarations. There is no specified default constructor for the class as both contained methods are static.
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.
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
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.
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.
The next section contains the namespace and class declarations, and default constructor.
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 the annotation.
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
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.