根据日期获取年龄

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>Documenttitle>
head>

<body>
    <script language=javascript>
	    // 在当前日之后,算0岁
		// 当前2023-04-09, 输入strAge 2023-04-10 ,输出为0
        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; //记得加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  "出生日期晚于今天,数据有误"; //返回-1 表示出生日期输入错误 晚于今天
                    }
                }
                return returnAge;
            } else {
                return ("输入的日期格式错误!");
            }
        }
		// 计算年龄,返回X岁X月X日
	// 输入参数1.出生日期,2.当前日期
	function calAge(birthday){
		var   strDate1   =   birthday + "   00:00:00.0";
		var d = new Date();
            var nowYear = d.getFullYear();
            var nowMonth = d.getMonth() + 1; //记得加1
            var nowDay = d.getDate();
		var   strDate2   =   nowYear+"-"+nowMonth+"-"+nowDay+ "   00:00:00.0";
		// new Date().format('Y-m-d') + "   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;
	}
	// 计算年龄,返回22.5(带小数的)
	// 输入参数1.出生日期,2.当前日期
	function calAgeDecimal(birthday){
		var   strDate1   =   birthday + "   00:00:00.0";
		var d = new Date();
        var nowYear = d.getFullYear();
        var nowMonth = d.getMonth() + 1; //记得加1
        var nowDay = d.getDate();
		var strDate2 = nowYear+"-"+nowMonth+"-"+nowDay+ "   00:00:00.0";
		// new Date().format('Y-m-d') + "   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>

你可能感兴趣的:(javascript,前端,html)