1 /** 2 * checkDate 3 */ 4 function DTO_checkDate(dateStr,format){ 5 if(dateStr == null || dateStr == "" ) return true; 6 if(dateStr.length != format.length){ 7 8 return false; 9 } 10 var year,month,day,hour,minute,second; 11 switch (format) { 12 case "YYYY" : 13 year = dateStr; 14 break; 15 case "YYYYMM" : 16 year = dateStr.substring(0,4); 17 month = dateStr.substring(4,6); 18 break; 19 case "YYYYMMDD" : 20 year = dateStr.substring(0,4); 21 month = dateStr.substring(4,6); 22 day = dateStr.substring(6,8); 23 break; 24 25 case "YYYYMMDD HH:MI" : 26 year = dateStr.substring(0,4); 27 month = dateStr.substring(4,6); 28 day = dateStr.substring(6,8); 29 hour = dateStr.substring(9,11); 30 minute = dateStr.substring(12,14); 31 break; 32 case "YYYYMMDD HH:MI:SS" : 33 year = dateStr.substring(0,4); 34 month = dateStr.substring(4,6); 35 day = dateStr.substring(6,8); 36 hour = dateStr.substring(9,11); 37 minute = dateStr.substring(12,14); 38 second = dateStr.substring(15,17); 39 break; 40 41 case "YYYY-MM" : 42 if(!_checkSpecial(dateStr,4,"-" )) return false; 43 year = dateStr.substring(0,4); 44 month = dateStr.substring(5,7); 45 break; 46 case "YYYY-MM-DD" : 47 if(!_checkSpecial(dateStr,4,"-" )) return false; 48 if(!_checkSpecial(dateStr,7,"-" )) return false; 49 year = dateStr.substring(0,4); 50 month = dateStr.substring(5,7); 51 day = dateStr.substring(8,10); 52 break; 53 case "YYYY-MM-DD HH:MI:SS" : 54 if(!_checkSpecial(dateStr,4,"-" )) return false; 55 if(!_checkSpecial(dateStr,7,"-" )) return false; 56 year = dateStr.substring(0,4); 57 month = dateStr.substring(5,7); 58 day = dateStr.substring(8,10); 59 hour = dateStr.substring(11,13); 60 minute = dateStr.substring(14,16); 61 second = dateStr.substring(17,19); 62 break; 63 case "YYYYMMDDHHMISS" : 64 year = dateStr.substring(0,4); 65 month = dateStr.substring(4,6); 66 day = dateStr.substring(6,8); 67 hour = dateStr.substring(8,10); 68 minute = dateStr.substring(10,12); 69 second = dateStr.substring(12,14); 70 break; 71 72 default: 73 74 return false ; 75 } 76 //check year 77 if(year != null){ 78 year = parseInt(year,10); 79 if(isNaN(year)){ 80 81 return false ; 82 } 83 if(year<1900 || year>2200){ 84 85 return false ; 86 } 87 88 } 89 //check month 90 if(month != null){ 91 month = parseInt(month,10); 92 if(isNaN(month)){ 93 94 return false ; 95 } 96 if(month<1 || month >12){ 97 98 return false ; 99 } 100 } 101 //check day 102 if(day != null){ 103 day = parseInt(day,10); 104 if(isNaN(day)){ 105 106 return false ; 107 } 108 if((day==0)||(day>31)){ 109 110 return false ; 111 } 112 else if (day>28 && day<31){ 113 if(month==2){ 114 if(day!=29){ 115 116 return false ; 117 } 118 else { 119 if((year%4)!=0){ 120 121 return false ; 122 } 123 else { 124 if((year%100==0)&&(year%400!=0)){ 125 126 return false ; 127 } 128 } 129 } 130 } 131 } 132 133 else if (day==31){ 134 if((month==2)||(month==4)||(month==6)||(month==9)||(month==11)){ 135 136 return false ; 137 } 138 } 139 } 140 //check hour 141 if(hour != null){ 142 hour = parseInt(hour,10); 143 if(isNaN(hour)){ 144 145 return false ; 146 } 147 if(hour<0 || hour >23){ 148 149 return false ; 150 } 151 } 152 //check minute 153 if(minute != null){ 154 minute = parseInt(minute,10); 155 if(isNaN(minute)){ 156 157 return false ; 158 } 159 if(minute<0 || minute >59){ 160 161 return false ; 162 } 163 } 164 //check second 165 if(second != null){ 166 second = parseInt(second,10); 167 if(isNaN(second)){ 168 169 return false ; 170 } 171 if(second<0 || second >59){ 172 173 return false ; 174 } 175 } 176 return true ; 177 } 178 function _checkSpecial(str,pos,identifier){ 179 if(str.substring(pos,pos+1) == identifier) return true; 180 pos++; 181 182 return false ; 183 }