<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script language=javascript>
function getAge(strAge) {
var birArr = strAge.split("-");
var birYear = birArr[0];
var birMonth = birArr[1];
var birDay = birArr[2];
d = new Date();
var nowYear = d.getFullYear();
var nowMonth = d.getMonth() + 1;
var nowDay = d.getDate();
var returnAge;
if (birArr == null) {
return false
};
var d = new Date(birYear, birMonth - 1, birDay);
if (d.getFullYear() == birYear && (d.getMonth() + 1) == birMonth && d.getDate() == birDay) {
if (nowYear == birYear) {
returnAge = 0;
} else {
var ageDiff = nowYear - birYear;
if (ageDiff > 0) {
if (nowMonth == birMonth) {
var dayDiff = nowDay - birDay;
if (dayDiff < 0) {
returnAge = ageDiff - 1;
} else {
returnAge = ageDiff;
}
} else {
var monthDiff = nowMonth - birMonth;
if (monthDiff < 0) {
returnAge = ageDiff - 1;
} else {
returnAge = ageDiff;
}
}
} else {
return "出生日期晚于今天,数据有误";
}
}
return returnAge;
} else {
return ("输入的日期格式错误!");
}
}
function calAge(birthday){
var strDate1 = birthday + " 00:00:00.0";
var d = new Date();
var nowYear = d.getFullYear();
var nowMonth = d.getMonth() + 1;
var nowDay = d.getDate();
var strDate2 = nowYear+"-"+nowMonth+"-"+nowDay+ " 00:00:00.0";
strDate1=strDate1.substring(0,strDate1.lastIndexOf(".")).replace(/-/g, "/ ");
strDate2=strDate2.substring(0,strDate2.lastIndexOf(".")).replace(/-/g, "/ ");
var date1 = Date.parse(strDate1);
var date2 = Date.parse(strDate2);
var day = Math.ceil((date2-date1)/(60*60*1000*24));
var age = '';
var year = Math.floor(day/365);
var y = day%365;
var month = Math.floor(y/30);
var d = Math.floor(day%365%30);
if(year > 0){
age += year + '岁';
}
if(month > 0){
age += month + '月';
}
if(d>0){
age += d+'天';
}
return age;
}
function calAgeDecimal(birthday){
var strDate1 = birthday + " 00:00:00.0";
var d = new Date();
var nowYear = d.getFullYear();
var nowMonth = d.getMonth() + 1;
var nowDay = d.getDate();
var strDate2 = nowYear+"-"+nowMonth+"-"+nowDay+ " 00:00:00.0";
if(strDate1.lastIndexOf(".") !=-1){
strDate1=strDate1.substring(0,strDate1.lastIndexOf(".")).replace(/-/g, "/ ");
} else{
strDate1=strDate1.replace(/-/g, "/ ");
}
strDate2=strDate2.substring(0,strDate2.lastIndexOf(".")).replace(/-/g, "/ ");
var date1 = Date.parse(strDate1);
var date2 = Date.parse(strDate2);
var day = Math.ceil((date2-date1)/(60*60*1000*24));
var age = null;
var year = Math.floor(day/365);
var y = day%365;
var month = Math.floor(y/30);
var d = Math.floor(day%365%30);
var monthDecimal = (month/12).toFixed(1);
age = parseFloat(year)+ parseFloat(monthDecimal);
return age;
}
console.log(getAge("2000-03-22"));
console.log(getAge("2000-06-02"));
console.log(getAge("2000-06-12"));
console.log(getAge("2022-04-10"));
console.log(getAge("2002-36-02"));
console.log(getAge("2026-02-02"));
console.log('calAge',calAge('2022-01-01'));
console.log('sss',calAgeDecimal("2023-03-02"));
</script>
</body>
</html>