1const calAge = (date) => new Date(Date.now() - new Date(date).getTime()).getFullYear() - 1970;
2
1// To calculate age:
2var year_born = prompt("Please enter your date of birth:", "Type here");
3var d = new Date();
4var n = d.getFullYear();
5function getAge(birthYear){
6 var currentDate = new Date();
7 var currentYear = currentDate.getFullYear();
8 age = currentYear - birthYear;
9 return age;
10}
11calculatedAge = getAge(year_born);
12alert("Hello, " + "you are " + calculatedAge + " years old!");
1function calculate_age(dob) {
2 var diff_ms = Date.now() - dob.getTime();
3 var age_dt = new Date(diff_ms);
4
5 return Math.abs(age_dt.getUTCFullYear() - 1970);
6}
7
8console.log(calculate_age(new Date(1982, 11, 4)));
9
10console.log(calculate_age(new Date(1962, 1, 1)));
11
12
1<!DOCTYPE html>
2<html>
3<head>
4 <meta charset="utf-8">
5 <title>JavaScript program to calculate age</title>
6</head>
7<body>
8
9</body>
10</html>
11
12