Calculation method of one year old (plus one year old on the day and one year old after the day)

Below is a problem that I encountered in my work about calculating a person’s age. There are two situations
. The first situation is to add 1 year on the day of the birthday.
The second situation is to add 1 year to the day after the birthday. I am
a newcomer. If you feel that there is a problem or a more common problem, leave a message and give an opinion

1. In the first case, add 1 year old on the birthday

//strBirthday为YYYY-MM-DD的格式
 function jsGetAge(strBirthday) {
    
    
                var returnAge;
                var strBirthdayArr;
                if (_.isString(strBirthday)) {
    
    
                    strBirthdayArr = strBirthday.split("-");
                } else if (_.isDate(strBirthday)) {
    
    
                    strBirthdayArr = [
                        strBirthday.getFullYear(),
                        strBirthday.getMonth() + 1,
                        strBirthday.getDate()
                    ];
                }
                var birthYear = strBirthdayArr[0];
                var birthMonth = strBirthdayArr[1];
                var birthDay = strBirthdayArr[2];
    
                var d = new Date();
                var nowYear = d.getFullYear();
                var nowMonth = d.getMonth() + 1;
                var nowDay = d.getDate();
    
                if (nowYear == birthYear) {
    
    
                    returnAge = 0;//同年 则为0岁
                } else {
    
    
                    var ageDiff = nowYear - birthYear; //年之差
                    if (ageDiff > 0) {
    
    
                        console.log(birthYear,birthMonth,birthDay,'现在',nowYear,nowMonth,nowDay,'年之差',ageDiff)
                        if (nowMonth == birthMonth) {
    
    
                            var dayDiff = nowDay - birthDay;//日之差
                            console.log(dayDiff);
                            // if (dayDiff <= 0) {
    
    
                            if (dayDiff < 0) {
    
    
                                returnAge = ageDiff - 1;
                            } else if(dayDiff > 0){
    
    
                                returnAge = ageDiff+1;
                            } else if(dayDiff == 0){
    
    
                                returnAge = ageDiff;
                            }
                        } else {
    
    
                            var monthDiff = nowMonth - birthMonth;//月之差
                            if (monthDiff < 0) {
    
    
                                returnAge = ageDiff - 1;
                            } else {
    
    
                                returnAge = ageDiff;
                            }
                        }
                    } else {
    
    
                        returnAge = -1;//返回-1 表示出生日期输入错误 晚于今天
                    }
                }
                return returnAge;//返回周岁年龄
            },

2. The second case is to add 1 year after the birthday

function jsGetAge(strBirthday) {
    
    
                var returnAge;
                var strBirthdayArr;
                if (_.isString(strBirthday)) {
    
    
                    strBirthdayArr = strBirthday.split("-");
                } else if (_.isDate(strBirthday)) {
    
    
                    strBirthdayArr = [
                        strBirthday.getFullYear(),
                        strBirthday.getMonth() + 1,
                        strBirthday.getDate()
                    ];
                }
                var birthYear = strBirthdayArr[0];
                var birthMonth = strBirthdayArr[1];
                var birthDay = strBirthdayArr[2];

                var d = new Date();
                var nowYear = d.getFullYear();
                var nowMonth = d.getMonth() + 1;
                var nowDay = d.getDate();

                if (nowYear == birthYear) {
    
    
                    returnAge = 0;//同年 则为0岁
                } else {
    
    
                    var ageDiff = nowYear - birthYear; //年之差
                    if (ageDiff > 0) {
    
    
                        if (nowMonth == birthMonth) {
    
    
                            var dayDiff = nowDay - birthDay;//日之差
                            // if (dayDiff <= 0) {
    
    
                            if (dayDiff < 0) {
    
    
                                returnAge = ageDiff - 1;
                            } else {
    
    
                                returnAge = ageDiff;
                            }
                        } else {
    
    
                            var monthDiff = nowMonth - birthMonth;//月之差
                            if (monthDiff < 0) {
    
    
                                returnAge = ageDiff - 1;
                            } else {
    
    
                                returnAge = ageDiff;
                            }
                        }
                    } else {
    
    
                        returnAge = -1;//返回-1 表示出生日期输入错误 晚于今天
                    }
                }
                return returnAge;//返回周岁年龄
            },

Guess you like

Origin blog.csdn.net/lbchenxy/article/details/97925668