1/* SOURCE FILE: date.js */ 2 3// HISTORY 4// ------------------------------------------------------------------ 5// May 17, 2003: Fixed bug in parseDate() for dates <1970 6// March 11, 2003: Added parseDate() function 7// March 11, 2003: Added "NNN" formatting option. Doesn't match up 8// perfectly with SimpleDateFormat formats, but 9// backwards-compatability was required. 10 11// ------------------------------------------------------------------ 12// These functions use the same 'format' strings as the 13// java.text.SimpleDateFormat class, with minor exceptions. 14// The format string consists of the following abbreviations: 15// 16// Field | Full Form | Short Form 17// -------------+--------------------+----------------------- 18// Year | yyyy (4 digits) | yy (2 digits), y (2 or 4 digits) 19// Month | MMM (name or abbr.)| MM (2 digits), M (1 or 2 digits) 20// | NNN (abbr.) | 21// Day of Month | dd (2 digits) | d (1 or 2 digits) 22// Day of Week | EE (name) | E (abbr) 23// Hour (1-12) | hh (2 digits) | h (1 or 2 digits) 24// Hour (0-23) | HH (2 digits) | H (1 or 2 digits) 25// Hour (0-11) | KK (2 digits) | K (1 or 2 digits) 26// Hour (1-24) | kk (2 digits) | k (1 or 2 digits) 27// Minute | mm (2 digits) | m (1 or 2 digits) 28// Second | ss (2 digits) | s (1 or 2 digits) 29// AM/PM | a | 30// 31// NOTE THE DIFFERENCE BETWEEN MM and mm! Month=MM, not mm! 32// Examples: 33// "MMM d, y" matches: January 01, 2000 34// Dec 1, 1900 35// Nov 20, 00 36// "M/d/yy" matches: 01/20/00 37// 9/2/00 38// "MMM dd, yyyy hh:mm:ssa" matches: "January 01, 2000 12:30:45AM" 39// ------------------------------------------------------------------ 40 41var MONTH_NAMES=new Array('January','February','March','April','May','June','July','August','September','October','November','December','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'); 42var DAY_NAMES=new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sun','Mon','Tue','Wed','Thu','Fri','Sat'); 43function LZ(x) {return(x<0||x>9?"":"0")+x} 44 45// ------------------------------------------------------------------ 46// isDate ( date_string, format_string ) 47// Returns true if date string matches format of format string and 48// is a valid date. Else returns false. 49// It is recommended that you trim whitespace around the value before 50// passing it to this function, as whitespace is NOT ignored! 51// ------------------------------------------------------------------ 52function isDate(val,format) { 53 var date=getDateFromFormat(val,format); 54 if (date==0) { return false; } 55 return true; 56 } 57 58// ------------------------------------------------------------------- 59// compareDates(date1,date1format,date2,date2format) 60// Compare two date strings to see which is greater. 61// Returns: 62// 1 if date1 is greater than date2 63// 0 if date2 is greater than date1 of if they are the same 64// -1 if either of the dates is in an invalid format 65// ------------------------------------------------------------------- 66function compareDates(date1,dateformat1,date2,dateformat2) { 67 var d1=getDateFromFormat(date1,dateformat1); 68 var d2=getDateFromFormat(date2,dateformat2); 69 if (d1==0 || d2==0) { 70 return -1; 71 } 72 else if (d1 > d2) { 73 return 1; 74 } 75 return 0; 76 } 77 78// ------------------------------------------------------------------ 79// formatDate (date_object, format) 80// Returns a date in the output format specified. 81// The format string uses the same abbreviations as in getDateFromFormat() 82// ------------------------------------------------------------------ 83function formatDate(date,format) { 84 format=format+""; 85 var result=""; 86 var i_format=0; 87 var c=""; 88 var token=""; 89 var y=date.getYear()+""; 90 var M=date.getMonth()+1; 91 var d=date.getDate(); 92 var E=date.getDay(); 93 var H=date.getHours(); 94 var m=date.getMinutes(); 95 var s=date.getSeconds(); 96 var yyyy,yy,MMM,MM,dd,hh,h,mm,ss,ampm,HH,H,KK,K,kk,k; 97 // Convert real date parts into formatted versions 98 var value=new Object(); 99 if (y.length < 4) {y=""+(y-0+1900);} 100 value["y"]=""+y; 101 value["yyyy"]=y; 102 value["yy"]=y.substring(2,4); 103 value["M"]=M; 104 value["MM"]=LZ(M); 105 value["MMM"]=MONTH_NAMES[M-1]; 106 value["NNN"]=MONTH_NAMES[M+11]; 107 value["d"]=d; 108 value["dd"]=LZ(d); 109 value["E"]=DAY_NAMES[E+7]; 110 value["EE"]=DAY_NAMES[E]; 111 value["H"]=H; 112 value["HH"]=LZ(H); 113 if (H==0){value["h"]=12;} 114 else if (H>12){value["h"]=H-12;} 115 else {value["h"]=H;} 116 value["hh"]=LZ(value["h"]); 117 if (H>11){value["K"]=H-12;} else {value["K"]=H;} 118 value["k"]=H+1; 119 value["KK"]=LZ(value["K"]); 120 value["kk"]=LZ(value["k"]); 121 if (H > 11) { value["a"]="PM"; } 122 else { value["a"]="AM"; } 123 value["m"]=m; 124 value["mm"]=LZ(m); 125 value["s"]=s; 126 value["ss"]=LZ(s); 127 while (i_format < format.length) { 128 c=format.charAt(i_format); 129 token=""; 130 while ((format.charAt(i_format)==c) && (i_format < format.length)) { 131 token += format.charAt(i_format++); 132 } 133 if (value[token] != null) { result=result + value[token]; } 134 else { result=result + token; } 135 } 136 return result; 137 } 138
139// ------------------------------------------------------------------ 140// Utility functions for parsing in getDateFromFormat() 141// ------------------------------------------------------------------ 142function _isInteger(val) { 143 var digits="1234567890"; 144 for (var i=0; i < val.length; i++) { 145 if (digits.indexOf(val.charAt(i))==-1) { return false; } 146 } 147 return true; 148 } 149function _getInt(str,i,minlength,maxlength) { 150 for (var x=maxlength; x>=minlength; x--) { 151 var token=str.substring(i,i+x); 152 if (token.length < minlength) { return null; } 153 if (_isInteger(token)) { return token; } 154 } 155 return null; 156 } 157 158// ------------------------------------------------------------------ 159// getDateFromFormat( date_string , format_string ) 160// 161// This function takes a date string and a format string. It matches 162// If the date string matches the format string, it returns the 163// getTime() of the date. If it does not match, it returns 0. 164// ------------------------------------------------------------------ 165function getDateFromFormat(val,format) { 166 val=val+""; 167 format=format+""; 168 var i_val=0; 169 var i_format=0; 170 var c=""; 171 var token=""; 172 var token2=""; 173 var x,y; 174 var now=new Date(); 175 var year=now.getYear(); 176 var month=now.getMonth()+1; 177 var date=1; 178 var hh=now.getHours(); 179 var mm=now.getMinutes(); 180 var ss=now.getSeconds(); 181 var ampm=""; 182 183 while (i_format < format.length) { 184 // Get next token from format string 185 c=format.charAt(i_format); 186 token=""; 187 while ((format.charAt(i_format)==c) && (i_format < format.length)) { 188 token += format.charAt(i_format++); 189 } 190 // Extract contents of value based on format token 191 if (token=="yyyy" || token=="yy" || token=="y") { 192 if (token=="yyyy") { x=4;y=4; } 193 if (token=="yy") { x=2;y=2; } 194 if (token=="y") { x=2;y=4; } 195 year=_getInt(val,i_val,x,y); 196 if (year==null) { return 0; } 197 i_val += year.length; 198 if (year.length==2) { 199 if (year > 70) { year=1900+(year-0); } 200 else { year=2000+(year-0); } 201 } 202 } 203 else if (token=="MMM"||token=="NNN"){ 204 month=0; 205 for (var i=0; i<MONTH_NAMES.length; i++) { 206 var month_name=MONTH_NAMES[i]; 207 if (val.substring(i_val,i_val+month_name.length).toLowerCase()==month_name.toLowerCase()) { 208 if (token=="MMM"||(token=="NNN"&&i>11)) { 209 month=i+1; 210 if (month>12) { month -= 12; } 211 i_val += month_name.length; 212 break; 213 } 214 } 215 } 216 if ((month < 1)||(month>12)){return 0;} 217 } 218 else if (token=="EE"||token=="E"){ 219 for (var i=0; i<DAY_NAMES.length; i++) { 220 var day_name=DAY_NAMES[i]; 221 if (val.substring(i_val,i_val+day_name.length).toLowerCase()==day_name.toLowerCase()) { 222 i_val += day_name.length; 223 break; 224 } 225 } 226 } 227 else if (token=="MM"||token=="M") { 228 month=_getInt(val,i_val,token.length,2); 229 if(month==null||(month<1)||(month>12)){return 0;} 230 i_val+=month.length;} 231 else if (token=="dd"||token=="d") { 232 date=_getInt(val,i_val,token.length,2); 233 if(date==null||(date<1)||(date>31)){return 0;} 234 i_val+=date.length;} 235 else if (token=="hh"||token=="h") { 236 hh=_getInt(val,i_val,token.length,2); 237 if(hh==null||(hh<1)||(hh>12)){return 0;} 238 i_val+=hh.length;} 239 else if (token=="HH"||token=="H") { 240 hh=_getInt(val,i_val,token.length,2); 241 if(hh==null||(hh<0)||(hh>23)){return 0;} 242 i_val+=hh.length;} 243 else if (token=="KK"||token=="K") { 244 hh=_getInt(val,i_val,token.length,2); 245 if(hh==null||(hh<0)||(hh>11)){return 0;} 246 i_val+=hh.length;} 247 else if (token=="kk"||token=="k") { 248 hh=_getInt(val,i_val,token.length,2); 249 if(hh==null||(hh<1)||(hh>24)){return 0;} 250 i_val+=hh.length;hh--;} 251 else if (token=="mm"||token=="m") { 252 mm=_getInt(val,i_val,token.length,2); 253 if(mm==null||(mm<0)||(mm>59)){return 0;} 254 i_val+=mm.length;} 255 else if (token=="ss"||token=="s") { 256 ss=_getInt(val,i_val,token.length,2); 257 if(ss==null||(ss<0)||(ss>59)){return 0;} 258 i_val+=ss.length;} 259 else if (token=="a") { 260 if (val.substring(i_val,i_val+2).toLowerCase()=="am") {ampm="AM";} 261 else if (val.substring(i_val,i_val+2).toLowerCase()=="pm") {ampm="PM";} 262 else {return 0;} 263 i_val+=2;} 264 else { 265 if (val.substring(i_val,i_val+token.length)!=token) {return 0;} 266 else {i_val+=token.length;} 267 } 268 } 269 // If there are any trailing characters left in the value, it doesn't match 270 if (i_val != val.length) { return 0; } 271 // Is date valid for month? 272 if (month==2) { 273 // Check for leap year 274 if ( ( (year%4==0)&&(year%100 != 0) ) || (year%400==0) ) { // leap year 275 if (date > 29){ return 0; } 276 } 277 else { if (date > 28) { return 0; } } 278 } 279 if ((month==4)||(month==6)||(month==9)||(month==11)) { 280 if (date > 30) { return 0; } 281 } 282 // Correct hours value 283 if (hh<12 && ampm=="PM") { hh=hh-0+12; } 284 else if (hh>11 && ampm=="AM") { hh-=12; } 285 var newdate=new Date(year,month-1,date,hh,mm,ss); 286 return newdate.getTime(); 287} 288 289// ------------------------------------------------------------------ 290// parseDate( date_string [, prefer_euro_format] ) 291// 292// This function takes a date string and tries to match it to a 293// number of possible date formats to get the value. It will try to 294// match against the following international formats, in this order: 295// y-M-d MMM d, y MMM d,y y-MMM-d d-MMM-y MMM d 296// M/d/y M-d-y M.d.y MMM-d M/d M-d 297// d/M/y d-M-y d.M.y d-MMM d/M d-M 298// A second argument may be passed to instruct the method to search 299// for formats like d/M/y (european format) before M/d/y (American). 300// Returns a Date object or null if no patterns match. 301// ------------------------------------------------------------------ 302function parseDate(val) { 303 var preferEuro=(arguments.length==2)?arguments[1]:false; 304 generalFormats=new Array('y-M-d','MMM d, y','MMM d,y','y-MMM-d','d-MMM-y','MMM d');
305 monthFirst=new Array('M/d/y','M-d-y','M.d.y','MMM-d','M/d','M-d'); 306 dateFirst =new Array('d/M/y','d-M-y','d.M.y','d-MMM','d/M','d-M'); 307 var checkList=new Array('generalFormats',preferEuro?'dateFirst':'monthFirst',preferEuro?'monthFirst':'dateFirst'); 308 var d=null; 309 for (var i=0; i<checkList.length; i++) { 310 var l=window[checkList[i]]; 311 for (var j=0; j<l.length; j++) { 312 d=getDateFromFormat(val,l[j]); 313 if (d!=0) { return new Date(d); } 314 } 315 } 316 return null; 317}
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.