DateTime - Age

A DateTime extension method that ages the given this.

Try it

public static void Main()
{
    DateTime date = new DateTime(2010, 1, 25);

    // C# Extension Method: DateTime - Age
	int age = date.Age();
	Console.WriteLine("Number of years (age): {0}", age);
}

View Source
using System;

public static partial class Extensions
{
    /// <summary>
    ///     A DateTime extension method that ages the given this.
    /// </summary>
    /// <param name="this">The @this to act on.</param>
    /// <returns>An int.</returns>
    public static int Age(this DateTime @this)
    {
        if (DateTime.Today.Month < @this.Month ||
            DateTime.Today.Month == @this.Month &&
            DateTime.Today.Day < @this.Day)
        {
            return DateTime.Today.Year - @this.Year - 1;
        }
        return DateTime.Today.Year - @this.Year;
    }
}