js年齡限制最大最小日期,對閏年進行判斷

佀無極發表於2018-09-10

2018年1月8日

js年齡限制最大最小日期,主要是閏年的判斷

var years, year, maxYear, month, twenty, ninety, minDay, maxDay, day_s;
years = Date.parse(new Date());
twenty = 365 * 24 * 3600 * 1000 * 20;//最小20歲
ninety = 365 * 24 * 3600 * 1000 * 90;//最大90歲
year = new Date(years - ninety).getFullYear();
maxYear = new Date(years - twenty).getFullYear();
month = new Date().getMonth() + 1;
day_s = new Date().getDate();
if(month < 10){
    month = `0` + month;
}
if(day_s < 10){
    day_s = `0` + day_s;
}
if ((year%4==0&&year%100!=0)||year%400==0){  
    if(month == 2){
        minDay = year + `-` + month + `-` + 29;
    }else{
        minDay = year + `-` + month + `-` + day_s;
    }
}else{
    if(month == 2){
        minDay = year + `-` + month + `-` + 28;
    }else{
        minDay = year + `-` + month + `-` + day_s;
    }
}
if ((maxYear%4==0&&maxYear%100!=0)||maxYear%400==0){  
    if(month == 2){
        maxDay = maxYear + `-` + month + `-` + 29;
    }else{
        maxDay = maxYear + `-` + month + `-` + day_s;
    }
}else{
    if(month == 2){
        maxDay = maxYear + `-` + month + `-` + 28;
    }else{
        maxDay = maxYear + `-` + month + `-` + day_s;
    }
}
console.log(minDay);
console.log(maxDay);


相關文章