取兩位數字,截取數字,求年齡,去除先後空格

//取兩位數
function toDou(n) {
    if(n < 10) {
        return '0' + n
    } else {
        return '' + n
    }
}
//截取數字
function getN(s) {
    if(s) {
        return s.replace(/[^0-9\.]/ig, "");
    }
}

  //去除先後空格
  String.prototype.trim = function() {
    return this.replace(/(^\s*)|(\s*$)/g, '');
  }this

//求年齡
function getAge(strBirthday) {
    var returnAge;
    var strBirthdayArr = strBirthday.split("-");
    var birthYear = strBirthdayArr[0];
    var birthMonth = strBirthdayArr[1];
    var birthDay = strBirthdayArr[2];
    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) {
                    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; //返回週歲年齡
}

老司機。。。spa

相關文章
相關標籤/搜索