Recently, in one of my projects I needed to calculate the age of a person and based on that the program would decide the next step. At first, I thought this is a straightforward process, all I needed to do is to subtract the person’s dob year from present year and voila ๐. Little did I know, it is more than that. Soon I realized that not only do I need to take into consideration the months and days but also the leap years. The reason we need to handle the leap years is because in a leap year there are 366 days vs to 365 days for a normal year. For example, if someone is born on Feb 29, his birthday would be on Feb 28 in a normal year (non leap year). This is because in a normal year February has 28 days not 29.
So, because I am generally a lazy person and I did not want to reinvent the wheel I decided on googling to find an implementation online. Unfortunatly, soon I understood that none of the implementations I found actually were addressing the leapers (born on Feb 29).
After the unsuccessful finds, I decided to implement it myself and add it to my
GitHub repository collection for future reference.
Here I present my implementation below, but first let me list the steps this code should accomplish.
- Be an extension to DateTime class.
- Calculate and return years between two dates.
- Calculate and return remaining months.
- Calculate and return remaining days.
- Handle Feb 29 and leap years.
To calculate the years component, we have to know two things. First, we need to find out if there is an extra day between start (dob) and present date. Second, we need to know whether dob has occurred in the present year. Once we know those two things, we can than calculate the years component by subtracting dob year from present year and an additional year if dob has not yet occur in the present year.
Once we implement the two things mentioned above and find the years component, the rest becomes pretty simple. By using the two flags and the years, we can now advance to prior or present year and calculate the remaining months and days. The code is simple and self-explanatory based on the information I provided so I will leave the explanation of each line of code for you to understand.
Source - https://github.com/arman-g/CalculateAge
Output
'L' - Leap Year, 'N' - Normal Year
-------- General Cases ---------
01/01/2000:L - 01/01/2000:L Age: 0yr., 0mos., 0d
01/01/2000:L - 01/02/2000:L Age: 0yr., 0mos., 1d
01/01/2000:L - 02/01/2000:L Age: 0yr., 1mos., 0d
01/01/2000:L - 12/31/2000:L Age: 0yr., 11mos., 30d
01/01/2000:L - 01/01/2001:N Age: 1yr., 0mos., 0d
04/22/2001:N - 10/27/2019:N Age: 18yr., 6mos., 5d
04/22/2001:N - 01/01/2020:L Age: 18yr., 8mos., 10d
04/22/2001:N - 02/17/2020:L Age: 18yr., 9mos., 26d
04/22/2001:N - 04/22/2020:L Age: 19yr., 0mos., 0d
04/22/2001:N - 07/07/2020:L Age: 19yr., 2mos., 15d
-------- Special Cases (Feb 29) ---------
02/29/1960:L - 02/28/2020:L Age: 59yr., 11mos., 30d
02/29/1960:L - 02/29/2020:L Age: 60yr., 0mos., 0d
02/29/1960:L - 03/01/2020:L Age: 60yr., 0mos., 1d
02/29/1960:L - 02/27/2021:N Age: 60yr., 11mos., 29d
02/29/1960:L - 02/28/2021:N Age: 61yr., 0mos., 0d
02/29/1960:L - 03/01/2021:N Age: 61yr., 0mos., 1d