[ Index ] |
|
Code source de b2evolution 2.1.0-beta |
1 // =================================================================== 2 // Author: Matt Kruse <matt@mattkruse.com> 3 // WWW: http://www.mattkruse.com/ 4 // 5 // NOTICE: You may use this code for any purpose, commercial or 6 // private, without any further permission from the author. You may 7 // remove this notice from your final code if you wish, however it is 8 // appreciated by the author if at least my web site address is kept. 9 // 10 // You may *NOT* re-distribute this code in any way except through its 11 // use. That means, you can include it in your product, or your web 12 // site, or any other form where the code is actually being used. You 13 // may not put the plain javascript up on your site for download or 14 // include it in your javascript libraries for download. 15 // If you wish to share this code with others, please just point them 16 // to the URL instead. 17 // Please DO NOT link directly to my .js files from your site. Copy 18 // the files to your server and use them there. Thank you. 19 // =================================================================== 20 21 // HISTORY 22 // ------------------------------------------------------------------ 23 // Feb 25, 2007: Added support for "S" (ordinal day number) (blueyed) 24 // Sep 26, 2006: Handle escaped chars in format string, e.g. "\d" as 25 // literal "d" (blueyed) 26 // May 17, 2003: Fixed bug in parseDate() for dates <1970 27 // March 11, 2003: Added parseDate() function 28 // March 11, 2003: Added "NNN" formatting option. Doesn't match up 29 // perfectly with SimpleDateFormat formats, but 30 // backwards-compatability was required. 31 32 // ------------------------------------------------------------------ 33 // These functions use the same 'format' strings as the 34 // java.text.SimpleDateFormat class, with minor exceptions. 35 // The format string consists of the following abbreviations: 36 // 37 // Field | Full Form | Short Form 38 // -------------+--------------------+----------------------- 39 // Year | yyyy (4 digits) | yy (2 digits), y (2 or 4 digits) 40 // Month | MMM (name or abbr.)| MM (2 digits), M (1 or 2 digits) 41 // | NNN (abbr.) | 42 // Day of Month | dd (2 digits) | d (1 or 2 digits) 43 // Day of Week | EE (name) | E (abbr) 44 // Hour (1-12) | hh (2 digits) | h (1 or 2 digits) 45 // Hour (0-23) | HH (2 digits) | H (1 or 2 digits) 46 // Hour (0-11) | KK (2 digits) | K (1 or 2 digits) 47 // Hour (1-24) | kk (2 digits) | k (1 or 2 digits) 48 // Minute | mm (2 digits) | m (1 or 2 digits) 49 // Second | ss (2 digits) | s (1 or 2 digits) 50 // AM/PM | a | 51 // day suffix | | 52 // (st|nd|rd|th)| S | 53 // 54 // NOTE THE DIFFERENCE BETWEEN MM and mm! Month=MM, not mm! 55 // Examples: 56 // "MMM d, y" matches: January 01, 2000 57 // Dec 1, 1900 58 // Nov 20, 00 59 // "M/d/yy" matches: 01/20/00 60 // 9/2/00 61 // "MMM dd, yyyy hh:mm:ssa" matches: "January 01, 2000 12:30:45AM" 62 // ------------------------------------------------------------------ 63 64 var 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'); 65 var DAY_NAMES=new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sun','Mon','Tue','Wed','Thu','Fri','Sat'); 66 function LZ(x) {return(x<0||x>9?"":"0")+x} 67 68 // ------------------------------------------------------------------ 69 // isDate ( date_string, format_string ) 70 // Returns true if date string matches format of format string and 71 // is a valid date. Else returns false. 72 // It is recommended that you trim whitespace around the value before 73 // passing it to this function, as whitespace is NOT ignored! 74 // ------------------------------------------------------------------ 75 function isDate(val,format) { 76 var date=getDateFromFormat(val,format); 77 if (date==0) { return false; } 78 return true; 79 } 80 81 // ------------------------------------------------------------------- 82 // compareDates(date1,date1format,date2,date2format) 83 // Compare two date strings to see which is greater. 84 // Returns: 85 // 1 if date1 is greater than date2 86 // 0 if date2 is greater than date1 of if they are the same 87 // -1 if either of the dates is in an invalid format 88 // ------------------------------------------------------------------- 89 function compareDates(date1,dateformat1,date2,dateformat2) { 90 var d1=getDateFromFormat(date1,dateformat1); 91 var d2=getDateFromFormat(date2,dateformat2); 92 if (d1==0 || d2==0) { 93 return -1; 94 } 95 else if (d1 > d2) { 96 return 1; 97 } 98 return 0; 99 } 100 101 // ------------------------------------------------------------------ 102 // formatDate (date_object, format) 103 // Returns a date in the output format specified. 104 // The format string uses the same abbreviations as in getDateFromFormat() 105 // ------------------------------------------------------------------ 106 function formatDate(date,format) { 107 format=format+""; 108 var result=""; 109 var i_format=0; 110 var c=""; 111 var token=""; 112 var y=date.getYear()+""; 113 var M=date.getMonth()+1; 114 var d=date.getDate(); 115 var E=date.getDay(); 116 var H=date.getHours(); 117 var m=date.getMinutes(); 118 var s=date.getSeconds(); 119 var yyyy,yy,MMM,MM,dd,hh,h,mm,ss,ampm,HH,H,KK,K,kk,k; 120 // Convert real date parts into formatted versions 121 var value=new Object(); 122 if (y.length < 4) {y=""+(y-0+1900);} 123 value["y"]=""+y; 124 value["yyyy"]=y; 125 value["yy"]=y.substring(2,4); 126 value["M"]=M; 127 value["MM"]=LZ(M); 128 value["MMM"]=MONTH_NAMES[M-1]; 129 value["NNN"]=MONTH_NAMES[M+11]; 130 value["d"]=d; 131 value["dd"]=LZ(d); 132 value["E"]=DAY_NAMES[E+7]; 133 value["EE"]=DAY_NAMES[E]; 134 value["H"]=H; 135 value["HH"]=LZ(H); 136 if (H==0){value["h"]=12;} 137 else if (H>12){value["h"]=H-12;} 138 else {value["h"]=H;} 139 value["hh"]=LZ(value["h"]); 140 if (H>11){value["K"]=H-12;} else {value["K"]=H;} 141 value["k"]=H+1; 142 value["KK"]=LZ(value["K"]); 143 value["kk"]=LZ(value["k"]); 144 if (H > 11) { value["a"]="PM"; } 145 else { value["a"]="AM"; } 146 value["m"]=m; 147 value["mm"]=LZ(m); 148 value["s"]=s; 149 value["ss"]=LZ(s); 150 151 // english ordinal numbers (see http://en.wikipedia.org/wiki/Names_of_numbers_in_English#Ordinal_numbers): 152 if( d < 20 && d > 9 ) 153 { 154 value["S"] = "th"; 155 } 156 else switch( d % 10 ) 157 { 158 case 1: value["S"] = "st"; break; 159 case 2: value["S"] = "nd"; break; 160 case 3: value["S"] = "rd"; break; 161 default: value["S"] = "th"; break; 162 } 163 164 while (i_format < format.length) { 165 c=format.charAt(i_format); 166 if( c == "\\" ) 167 { // Use escaped char as-is: 168 i_format++; 169 if( i_format < format.length ) { 170 result=result + format.charAt(i_format); 171 i_format++; 172 } 173 continue; 174 } 175 token=""; 176 while ((format.charAt(i_format)==c) && (i_format < format.length)) { 177 token += format.charAt(i_format++); 178 } 179 if (value[token] != null) { result=result + value[token]; } 180 else { result=result + token; } 181 } 182 return result; 183 } 184 185 // ------------------------------------------------------------------ 186 // Utility functions for parsing in getDateFromFormat() 187 // ------------------------------------------------------------------ 188 function _isInteger(val) { 189 var digits="1234567890"; 190 for (var i=0; i < val.length; i++) { 191 if (digits.indexOf(val.charAt(i))==-1) { return false; } 192 } 193 return true; 194 } 195 function _getInt(str,i,minlength,maxlength) { 196 for (var x=maxlength; x>=minlength; x--) { 197 var token=str.substring(i,i+x); 198 if (token.length < minlength) { return null; } 199 if (_isInteger(token)) { return token; } 200 } 201 return null; 202 } 203 204 // ------------------------------------------------------------------ 205 // getDateFromFormat( date_string , format_string ) 206 // 207 // This function takes a date string and a format string. It matches 208 // If the date string matches the format string, it returns the 209 // getTime() of the date. If it does not match, it returns 0. 210 // ------------------------------------------------------------------ 211 function getDateFromFormat(val,format) { 212 val=val+""; 213 format=format+""; 214 var i_val=0; 215 var i_format=0; 216 var c=""; 217 var token=""; 218 var token2=""; 219 var x,y; 220 var now=new Date(); 221 var year=now.getYear(); 222 var month=now.getMonth()+1; 223 var date=1; 224 var hh=now.getHours(); 225 var mm=now.getMinutes(); 226 var ss=now.getSeconds(); 227 var ampm=""; 228 229 while (i_format < format.length) { 230 // Get next token from format string 231 c=format.charAt(i_format); 232 233 if( c == "\\" ) 234 { // Consume escaped char: 235 i_format++; 236 if( i_format < format.length ) { 237 if( format.charAt(i_format) != val.charAt(i_val) ) 238 { // Escaped char not found: 239 return 0; 240 } 241 i_format++; 242 } 243 i_val++; 244 continue; 245 } 246 247 token=""; 248 while ((format.charAt(i_format)==c) && (i_format < format.length)) { 249 token += format.charAt(i_format++); 250 } 251 // Extract contents of value based on format token 252 if (token=="yyyy" || token=="yy" || token=="y") { 253 if (token=="yyyy") { x=4;y=4; } 254 if (token=="yy") { x=2;y=2; } 255 if (token=="y") { x=2;y=4; } 256 year=_getInt(val,i_val,x,y); 257 if (year==null) { return 0; } 258 i_val += year.length; 259 if (year.length==2) { 260 if (year > 70) { year=1900+(year-0); } 261 else { year=2000+(year-0); } 262 } 263 } 264 else if (token=="MMM"||token=="NNN"){ 265 month=0; 266 for (var i=0; i<MONTH_NAMES.length; i++) { 267 var month_name=MONTH_NAMES[i]; 268 if (val.substring(i_val,i_val+month_name.length).toLowerCase()==month_name.toLowerCase()) { 269 if (token=="MMM"||(token=="NNN"&&i>11)) { 270 month=i+1; 271 if (month>12) { month -= 12; } 272 i_val += month_name.length; 273 break; 274 } 275 } 276 } 277 if ((month < 1)||(month>12)){return 0;} 278 } 279 else if (token=="EE"||token=="E"){ 280 for (var i=0; i<DAY_NAMES.length; i++) { 281 var day_name=DAY_NAMES[i]; 282 if (val.substring(i_val,i_val+day_name.length).toLowerCase()==day_name.toLowerCase()) { 283 i_val += day_name.length; 284 break; 285 } 286 } 287 } 288 else if (token=="MM"||token=="M") { 289 month=_getInt(val,i_val,token.length,2); 290 if(month==null||(month<1)||(month>12)){return 0;} 291 i_val+=month.length;} 292 else if (token=="dd"||token=="d") { 293 date=_getInt(val,i_val,token.length,2); 294 if(date==null||(date<1)||(date>31)){return 0;} 295 i_val+=date.length;} 296 else if (token=="hh"||token=="h") { 297 hh=_getInt(val,i_val,token.length,2); 298 if(hh==null||(hh<1)||(hh>12)){return 0;} 299 i_val+=hh.length;} 300 else if (token=="HH"||token=="H") { 301 hh=_getInt(val,i_val,token.length,2); 302 if(hh==null||(hh<0)||(hh>23)){return 0;} 303 i_val+=hh.length;} 304 else if (token=="KK"||token=="K") { 305 hh=_getInt(val,i_val,token.length,2); 306 if(hh==null||(hh<0)||(hh>11)){return 0;} 307 i_val+=hh.length;} 308 else if (token=="kk"||token=="k") { 309 hh=_getInt(val,i_val,token.length,2); 310 if(hh==null||(hh<1)||(hh>24)){return 0;} 311 i_val+=hh.length;hh--;} 312 else if (token=="mm"||token=="m") { 313 mm=_getInt(val,i_val,token.length,2); 314 if(mm==null||(mm<0)||(mm>59)){return 0;} 315 i_val+=mm.length;} 316 else if (token=="ss"||token=="s") { 317 ss=_getInt(val,i_val,token.length,2); 318 if(ss==null||(ss<0)||(ss>59)){return 0;} 319 i_val+=ss.length;} 320 else if (token=="a") { 321 if (val.substring(i_val,i_val+2).toLowerCase()=="am") {ampm="AM";} 322 else if (val.substring(i_val,i_val+2).toLowerCase()=="pm") {ampm="PM";} 323 else {return 0;} 324 i_val+=2;} 325 else { 326 if (val.substring(i_val,i_val+token.length)!=token) {return 0;} 327 else {i_val+=token.length;} 328 } 329 } 330 // If there are any trailing characters left in the value, it doesn't match 331 if (i_val != val.length) { return 0; } 332 // Is date valid for month? 333 if (month==2) { 334 // Check for leap year 335 if ( ( (year%4==0)&&(year%100 != 0) ) || (year%400==0) ) { // leap year 336 if (date > 29){ return 0; } 337 } 338 else { if (date > 28) { return 0; } } 339 } 340 if ((month==4)||(month==6)||(month==9)||(month==11)) { 341 if (date > 30) { return 0; } 342 } 343 // Correct hours value 344 if (hh<12 && ampm=="PM") { hh=hh-0+12; } 345 else if (hh>11 && ampm=="AM") { hh-=12; } 346 var newdate=new Date(year,month-1,date,hh,mm,ss); 347 return newdate.getTime(); 348 } 349 350 // ------------------------------------------------------------------ 351 // parseDate( date_string [, prefer_euro_format] ) 352 // 353 // This function takes a date string and tries to match it to a 354 // number of possible date formats to get the value. It will try to 355 // match against the following international formats, in this order: 356 // y-M-d MMM d, y MMM d,y y-MMM-d d-MMM-y MMM d 357 // M/d/y M-d-y M.d.y MMM-d M/d M-d 358 // d/M/y d-M-y d.M.y d-MMM d/M d-M 359 // A second argument may be passed to instruct the method to search 360 // for formats like d/M/y (european format) before M/d/y (American). 361 // Returns a Date object or null if no patterns match. 362 // ------------------------------------------------------------------ 363 function parseDate(val) { 364 var preferEuro=(arguments.length==2)?arguments[1]:false; 365 generalFormats=new Array('y-M-d','MMM d, y','MMM d,y','y-MMM-d','d-MMM-y','MMM d'); 366 monthFirst=new Array('M/d/y','M-d-y','M.d.y','MMM-d','M/d','M-d'); 367 dateFirst =new Array('d/M/y','d-M-y','d.M.y','d-MMM','d/M','d-M'); 368 var checkList=new Array('generalFormats',preferEuro?'dateFirst':'monthFirst',preferEuro?'monthFirst':'dateFirst'); 369 var d=null; 370 for (var i=0; i<checkList.length; i++) { 371 var l=window[checkList[i]]; 372 for (var j=0; j<l.length; j++) { 373 d=getDateFromFormat(val,l[j]); 374 if (d!=0) { return new Date(d); } 375 } 376 } 377 return null; 378 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Thu Nov 29 23:58:50 2007 | par Balluche grâce à PHPXref 0.7 |
![]() |