根据日期获取年龄

<!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>
	    // 在当前日之后,算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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值