age calculator javascript w3schools code example
Example 1: how to get the year in javascript
new Date().getFullYear();
Example 2: age calculator javascript
function calculate_age(dob) {
var diff_ms = Date.now() - dob.getTime();
var age_dt = new Date(diff_ms);
return Math.abs(age_dt.getUTCFullYear() - 1970);
}
console.log(calculate_age(new Date(1982, 11, 4)));
console.log(calculate_age(new Date(1962, 1, 1)));
Example 3: get year from date javascript
document.write(new Date().getFullYear());
Example 4: age calculator javascript
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JavaScript program to calculate age</title>
</head>
<body>
</body>
</html>