javascript age code example
Example 1: javascript get age
function getAge(dateString)
{
var today = new Date();
var birthDate = new Date(dateString);
var age = today.getFullYear() - birthDate.getFullYear();
var m = today.getMonth() - birthDate.getMonth();
if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate()))
{
age--;
}
return age;
}
Example 2: how to code a check age function in javascript
function Person(dob) {
this.birthday = new Date(dob);
this.calculateAge = function() {
const diff = Date.now() - this.birthday.getTime();
const ageDate = new Date(diff);
console.log(ageDate.getUTCFullYear());
return Math.abs(ageDate.getUTCFullYear() - 1970);
};
}
var age =new Person('2000-1-1').calculateAge();
console.log(age);
Example 3: javascript calculate age
calculate Age using Javascript