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"
)
Date.prototype.age=function(at){
var value = new Date(this.getTime());
var age = at.getFullYear() - value.getFullYear();
value = value.setFullYear(at.getFullYear());
if (at < value) --age;
return age;
};
var dob = new Date(Date.parse($(this).text()));
if(dob.age(new Date()) < 18)
{
$(this).text("Under 18");
}
else
{
$(this).text(" Over 18");
}