Extension to calculate age

using System;

namespace Extensions
{
    public static class DateTimeExtensions
    {
        public static int Age(this DateTime birthDate)
        {
            return Age(birthDate, DateTime.Today);
        }

        public static int Age(this DateTime birthDateTime, DateTime atDateTime)
        {
            var birthDate = birthDateTime.Date;
            var atDate = atDateTime.Date;
            var age = atDate.Year - birthDate.Year;

            if (birthDate.AddYears(age) > atDate)
            {
                age--;
            }

            return age;
        }
    }
}

Something to add?

This site uses Akismet to reduce spam. Learn how your comment data is processed.