//--- 摘自 http://www.csdn.net/expert/topic/791/791385.xml?temp=.1471521
//--- 作者: xg_delayth//--- 方法名: judgeInputDate(AYear,AMonth,ADay)
//--- 功能: 判断输入的年、月、日是否合法,时间范围在1900~2100年之间
//--- 返回: 合法返回1,不合法返回0 
//---     AYear : 输入的年份
//---     AMonth: 输入的月份
//---     ADay  : 输入的日子
//--- 创建日期:         2001-06-27     最近更新
function judgeInputDate(AYear,AMonth,ADay){
    var tempYear,tempMonth,tempDay;
    //--- 非闰年的每个月天数 
    var constMonth = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
    //--- 一般输入性判断  
    if ((AYear.length > 4) || (AMonth.length > 2)){return 0;}
    if ((judgeStringType(AYear,"iNChar") == false) || (judgeStringType(AMonth,"iNChar") == false) 
      || (judgeStringType(ADay,"iNChar") == false)){ return 0 }
    //--- 取得输入值  
    tempYear = parseInt(AYear);
    if((tempYear > 2100) || (tempYear < 1900)){return 0;}  
    tempMonth = parseInt(trimSubStr(AMonth,"0",1));
    tempDay = parseInt(trimSubStr(ADay,"0",1));
    //--- 判断月合法性
    if ((tempMonth == 0) || (tempMonth > 12)){return 0;}
    if (tempMonth != 2){
      if((tempDay > constMonth[tempMonth - 1]) || (tempDay == 0)){return 0;}
    } else{ //--- 根据闰年判断
      if((tempYear % 4 == 0) && (tempYear % 100 != 0) || (tempYear % 400 == 0)){
        if((tempDay > 29) || (tempDay == 0)){return 0;}
      }else{
        if((tempDay > constMonth[1]) || (tempDay == 0)){return 0;}
      }  
    } 
    return 1 
  }

解决方案 »

  1.   

    好复杂的判断,我都是new一个Date对象出来,setFullYear,setMonth,setDate,再get回来比较是不是一致的。
      

  2.   

    Dear  emu(ston):請問具體方法?
      

  3.   

    http://www.blueidea.com/bbs/newsdetail.asp?id=472344<script>
    function strDateTime(str){
    var reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/; 
    var r = str.match(reg); 
    if(r==null)return false; 
    var d= new Date(r[1], r[3]-1,r[4],r[5],r[6],r[7]); 
    var newStr=d.getFullYear()+r[2]+(d.getMonth()+1)+r[2]+d.getDate()+" "+d.getHours()+":"+d.getMinutes()+":"+d.getSeconds()
    return newStr==str
    }
    alert(strDateTime("2002-1-31 12:34:56"))
    alert(strDateTime("2002-1-31 12:54:56"))
    alert(strDateTime("2002-1-41 12:00:00"))
    </script>
    ps.大家收藏或整理到faq,我不想再回同样的问题,呵呵