Date.js 12 KB

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