Javascript判定日付フォーマット

606 ワード

 function isValidDate(str) {  
      if (!/^\d{4}\-\d\d?\-\d\d?/.test(str)) {  
          return false;  
      }  
      var array = str.replace(/\-0/g, "-").split("-");  
      var year = parseInt(array[0]);  
      var month = parseInt(array[1]) - 1;  
      var day = parseInt(array[2]);  
      var date = new Date(year, month, day);  
      return (date.getFullYear() == year &&   
              date.getMonth() == month &&   
              date.getDate() == day);  
  }