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);

 

 

posted @ 2014-05-22 22:20  Hypocrite  阅读(3604)  评论(0编辑  收藏  举报