📜  javascript 获取年龄 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:01:08.932000             🧑  作者: Mango

代码示例2
// To calculate age:
var year_born = prompt("Please enter your date of birth:", "Type here");
var d = new Date();
var n = d.getFullYear();
function getAge(birthYear){
    var currentDate = new Date();
    var currentYear = currentDate.getFullYear();
    age = currentYear - birthYear;
    return age;
}
calculatedAge = getAge(year_born);
alert("Hello, " + "you are " + calculatedAge + " years old!");