Today I had a requirement to calculate someone’s age based on that person’s birthday. It seems like a pretty simple function to write, but I decided to Google around a little bit anyway to see if what other people were doing to calculate age based on birthday. Turns out, a few people have ideas about this. The first Google result is a Stack Overflow post titled How do I calculate someone’s age based on a DateTime type birthday? that is full of ways to calculate the age. The post is over 13 years old but is plum full with so many different methods of calculating an age based off of a birthday.
Below are two ways that I found to be popular in the post as well as a pretty cool library that calculates age in C# with a single method call.
//TLDR; There is a great .Net 5+ library called AgeCalculator that calculates age based off of two DateTime values. Unfortunately, as far as I can tell the AgeCalculator library is only available for .Net 5+. So, in if you’re on an older version of .Net you should take a look at this Stack Overflow thread or the second example below for ways to calculate age in C#.
Calculating age based on year alone
My initial thought was to find age by subtracting the birthday year from today’s year. However, I quickly realized that doesn’t work so well, unless the two dates are the exact same day. Since that is not the case, this method does not take into account the offset in days and months as well as other factors(leap years, etc) so this is not best way to make this calculation.
using System; public class Program { public static void Main() { var birthday = new DateTime(1980,12,11); var age = DateTime.Now.Year - birthday.Year; Console.WriteLine(age); //Output: 42 } }
Calculating age based on number of days
The method that seems to work well is by James Current from this Stack Overflow comment which takes into consideration the days/months between now and the birthday. This seems to be the preferred method to return the accurate age based off the birthday. If you cannot use the AgeCalculator package for .Net 5+ this could be a valid option.
using System; public class Program { public static void Main() { var birthday = new DateTime(1980,12,11); int age = (int) ((DateTime.Now - birthday).TotalDays/365.242199); Console.WriteLine(age); //Output: 41 } }
Calculating age using AgeCalculator library
For .Net 5+ code bases you can use this AgeCalculator library. As you can see, you have access immediately to the age based off a new object’s constructor.
using System; public class Program { public static void Main() { var birthday = new DateTime(1980,12,11); var age = new Age(birthday, DateTime.Today); Console.WriteLine(age.Years); //Output: 41 } }
Other ways to use this library
Based on the documentation of the library there are a whole lot of ways you can use this to calculate age. Here are just a few ways that I took from the README file on the AgeCalculator library’s Github page. It seems like a pretty extensible library to use for dates, I’m excited to use this in more projects.
/* There are three ways to calculate the age between two dates */ using AgeCalculator; using AgeCalculator.Extensions;public void PrintMyAge() { // Date of birth or from date. var dob = DateTime.Parse("10/03/2015");
// #1. Using the Age class constructor. var age = new Age(dob, DateTime.Today); // as of 09/19/2021 Console.WriteLine($"Age: {age.Years} years, {age.Months} months, {age.Days} days, {age.Time}"); // #2. Using DateTime extension. age = dob.CalculateAge(DateTime.Today); // as of 09/19/2021 Console.WriteLine($"Age: {age.Years} years, {age.Months} months, {age.Days} days, {age.Time}"); // #3. Using the Age type's static function. age = Age.Calculate(dob, DateTime.Today); // as of 09/19/2021 Console.WriteLine($"Age: {age.Years} years, {age.Months} months, {age.Days} days, {age.Time}");
}
// Output: // Age: 5 years, 11 months, 16 days, 00:00:00