Age from Date of Birth using JQuery

You might find the open source Datejs library to be helpful. Specifically the the addYears function.

var dob = Date.parse($(this).text());
if (dob.addYears(18) < Date.today())
{
    $(this).text("Under 18");
}
else
{
    $(this).text(" Over 18");
}

In a more terse fashion:

$(this).text(
    Date.parse($(this).text()).addYears(18) < Date.today() ?
    "Under 18" :
    " Over 18"
)

Leave a Comment