Javascript Date 判断输入日期是否正确

JavaScript的Date对象有容错性,可将随意给定的日期的年月日自动生成正确的日期时间

//JavaScript中Date对象容错性
function dateCheck(){
    var date = new Date();
    date.setDate(date.getDate()+13);
    //date.setDate(date.getMonth()+1+10);
    //打印依然能输出正确的日期
    console.log(date.getFullYear()+"年"+(date.getDate())+"日"+(date.getMonth()+1)+"月");
}

由此可以根据Date对象的容错性对输入日期的年月日进行正确性判断,具体代码如下:

//判断给定日期是否合法
function checkDate(year,month,date){
    var now = new Date(year,month -1,date);
    if(now.getDate()===date&&now.getFullYear()==year&&now.getMonth()===(month-1)){
        return true;
    }
    return false;
}
checkDate(2014,8,34);
原文地址:https://www.cnblogs.com/title/p/3735966.html