Calculate Age in C# or JavaScript

A simple method to calculate age in years in C# and JavaScript.  Additional info on the number of days in a year can be found here.

C#

public static double CalculateAge(DateTime birthDate)
{
    double daysPerYear = 365.242199;
    TimeSpan span = DateTime.Now.Subtract(birthDate);
    double age = span.TotalDays / daysPerYear;

    return age;
}

JavaScript

function CalculateAge( birthDate )
{
    var msPerDay = 1000 * 60 * 60 * 24;
    var daysPerYear = 365.242199;
    var age = ( new Date() - new Date(birthDate) ) / msPerDay / daysPerYear;

    return age;
}

Tags: , , ,

  • Delicious
  • Facebook
  • Digg
  • Reddit
  • StumbleUpon
  • Twitter

One Response to “Calculate Age in C# or JavaScript”

  1. Clarisa Espy says:

    I found this information usefull.

Leave a Reply