Home  >  Article  >  Web Front-end  >  JS implements method to get age based on date of birth

JS implements method to get age based on date of birth

小云云
小云云Original
2017-12-13 15:10:064142browse


In this article, we mainly share with you the JS method to obtain age based on date of birth, hoping to help everyone.

JavaScript//JS根据出生日期 得到年龄                 //参数strBirthday已经是正确格式的2017-12-12这样的日期字符串  function jsGetAge(strBirthday)  {         
    var returnAge;  
    var strBirthdayArr=strBirthday.split("-");  
    var birthYear = strBirthdayArr[0];  
    var birthMonth = strBirthdayArr[1];  
    var birthDay = strBirthdayArr[2];  
    var d = new Date();  
    var nowYear = d.getYear();  
    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)  
                {  
                    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;//返回周岁年龄  }

                                                                                                             


JavaScript//JS根据出生日期 得到年龄                 //参数strBirthday已经是正确格式的2017-12-12这样的日期字符串  function jsGetAge(strBirthday)  {         
    var returnAge;  
    var strBirthdayArr=strBirthday.split("-");  
    var birthYear = strBirthdayArr[0];  
    var birthMonth = strBirthdayArr[1];  
    var birthDay = strBirthdayArr[2];  

    var d = new Date();  
    var nowYear = d.getYear();  
    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)  
                {  
                    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;//返回周岁年龄  }

Related recommendations:

MYSQL date function summary

JS implementation of the case of comparing the number of days between two dates

Detailed example of how mysql automatically obtains the time and date

The above is the detailed content of JS implements method to get age based on date of birth. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn