dwz.util.date.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /**
  2. * @author ZhangHuihua@msn.com
  3. * ----------------------------------------------------------
  4. * These functions use the same 'format' strings as the
  5. * java.text.SimpleDateFormat class, with minor exceptions.
  6. * The format string consists of the following abbreviations:
  7. *
  8. * Field | Full Form | Short Form
  9. * -------------+--------------------+-----------------------
  10. * Year | yyyy (4 digits) | yy (2 digits), y (2 or 4 digits)
  11. * Month | MMM (name or abbr.)| MM (2 digits), M (1 or 2 digits)
  12. * | NNN (abbr.) |
  13. * Day of Month | dd (2 digits) | d (1 or 2 digits)
  14. * Day of Week | EE (name) | E (abbr)
  15. * Hour (1-12) | hh (2 digits) | h (1 or 2 digits)
  16. * Hour (0-23) | HH (2 digits) | H (1 or 2 digits)
  17. * Hour (0-11) | KK (2 digits) | K (1 or 2 digits)
  18. * Hour (1-24) | kk (2 digits) | k (1 or 2 digits)
  19. * Minute | mm (2 digits) | m (1 or 2 digits)
  20. * Second | ss (2 digits) | s (1 or 2 digits)
  21. * AM/PM | a |
  22. *
  23. * NOTE THE DIFFERENCE BETWEEN MM and mm! Month=MM, not mm!
  24. * Examples:
  25. * "MMM d, y" matches: January 01, 2000
  26. * Dec 1, 1900
  27. * Nov 20, 00
  28. * "M/d/yy" matches: 01/20/00
  29. * 9/2/00
  30. * "MMM dd, yyyy hh:mm:ssa" matches: "January 01, 2000 12:30:45AM"
  31. * ----------------------------------------------------------
  32. */
  33. (function(){
  34. 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');
  35. var DAY_NAMES=new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sun','Mon','Tue','Wed','Thu','Fri','Sat');
  36. function LZ(x) {return(x<0||x>9?"":"0")+x}
  37. /**
  38. * formatDate (date_object, format)
  39. * Returns a date in the output format specified.
  40. * The format string uses the same abbreviations as in parseDate()
  41. * @param {Object} date
  42. * @param {Object} format
  43. */
  44. function formatDate(date,format) {
  45. format=format+"";
  46. var result="";
  47. var i_format=0;
  48. var c="";
  49. var token="";
  50. var y=date.getYear()+"";
  51. var M=date.getMonth()+1;
  52. var d=date.getDate();
  53. var E=date.getDay();
  54. var H=date.getHours();
  55. var m=date.getMinutes();
  56. var s=date.getSeconds();
  57. var yyyy,yy,MMM,MM,dd,hh,h,mm,ss,ampm,HH,H,KK,K,kk,k;
  58. // Convert real date parts into formatted versions
  59. var value={};
  60. if (y.length < 4) {y=""+(y-0+1900);}
  61. value["y"]=""+y;
  62. value["yyyy"]=y;
  63. value["yy"]=y.substring(2,4);
  64. value["M"]=M;
  65. value["MM"]=LZ(M);
  66. value["MMM"]=MONTH_NAMES[M-1];
  67. value["NNN"]=MONTH_NAMES[M+11];
  68. value["d"]=d;
  69. value["dd"]=LZ(d);
  70. value["E"]=DAY_NAMES[E+7];
  71. value["EE"]=DAY_NAMES[E];
  72. value["H"]=H;
  73. value["HH"]=LZ(H);
  74. if (H==0){value["h"]=12;}
  75. else if (H>12){value["h"]=H-12;}
  76. else {value["h"]=H;}
  77. value["hh"]=LZ(value["h"]);
  78. if (H>11){value["K"]=H-12;} else {value["K"]=H;}
  79. value["k"]=H+1;
  80. value["KK"]=LZ(value["K"]);
  81. value["kk"]=LZ(value["k"]);
  82. if (H > 11) { value["a"]="PM"; }
  83. else { value["a"]="AM"; }
  84. value["m"]=m;
  85. value["mm"]=LZ(m);
  86. value["s"]=s;
  87. value["ss"]=LZ(s);
  88. while (i_format < format.length) {
  89. c=format.charAt(i_format);
  90. token="";
  91. while ((format.charAt(i_format)==c) && (i_format < format.length)) {
  92. token += format.charAt(i_format++);
  93. }
  94. if (value[token] != null) { result += value[token]; }
  95. else { result += token; }
  96. }
  97. return result;
  98. }
  99. function _isInteger(val) {
  100. return (new RegExp(/^\d+$/).test(val));
  101. }
  102. function _getInt(str,i,minlength,maxlength) {
  103. for (var x=maxlength; x>=minlength; x--) {
  104. var token=str.substring(i,i+x);
  105. if (token.length < minlength) { return null; }
  106. if (_isInteger(token)) { return token; }
  107. }
  108. return null;
  109. }
  110. /**
  111. * parseDate( date_string , format_string )
  112. *
  113. * This function takes a date string and a format string. It matches
  114. * If the date string matches the format string, it returns the date.
  115. * If it does not match, it returns 0.
  116. * @param {Object} val
  117. * @param {Object} format
  118. */
  119. function parseDate(val,format) {
  120. val=val+"";
  121. format=format+"";
  122. var i_val=0;
  123. var i_format=0;
  124. var c="";
  125. var token="";
  126. var token2="";
  127. var x,y;
  128. var now=new Date(1900,0,1);
  129. var year=now.getYear();
  130. var month=now.getMonth()+1;
  131. var date=1;
  132. var hh=now.getHours();
  133. var mm=now.getMinutes();
  134. var ss=now.getSeconds();
  135. var ampm="";
  136. while (i_format < format.length) {
  137. // Get next token from format string
  138. c=format.charAt(i_format);
  139. token="";
  140. while ((format.charAt(i_format)==c) && (i_format < format.length)) {
  141. token += format.charAt(i_format++);
  142. }
  143. // Extract contents of value based on format token
  144. if (token=="yyyy" || token=="yy" || token=="y") {
  145. if (token=="yyyy") { x=4;y=4; }
  146. if (token=="yy") { x=2;y=2; }
  147. if (token=="y") { x=2;y=4; }
  148. year=_getInt(val,i_val,x,y);
  149. if (year==null) { return 0; }
  150. i_val += year.length;
  151. if (year.length==2) {
  152. if (year > 70) { year=1900+(year-0); }
  153. else { year=2000+(year-0); }
  154. }
  155. } else if (token=="MMM"||token=="NNN"){
  156. month=0;
  157. for (var i=0; i<MONTH_NAMES.length; i++) {
  158. var month_name=MONTH_NAMES[i];
  159. if (val.substring(i_val,i_val+month_name.length).toLowerCase()==month_name.toLowerCase()) {
  160. if (token=="MMM"||(token=="NNN"&&i>11)) {
  161. month=i+1;
  162. if (month>12) { month -= 12; }
  163. i_val += month_name.length;
  164. break;
  165. }
  166. }
  167. }
  168. if ((month < 1)||(month>12)){return 0;}
  169. } else if (token=="EE"||token=="E"){
  170. for (var i=0; i<DAY_NAMES.length; i++) {
  171. var day_name=DAY_NAMES[i];
  172. if (val.substring(i_val,i_val+day_name.length).toLowerCase()==day_name.toLowerCase()) {
  173. i_val += day_name.length;
  174. break;
  175. }
  176. }
  177. } else if (token=="MM"||token=="M") {
  178. month=_getInt(val,i_val,token.length,2);
  179. if(month==null||(month<1)||(month>12)){return 0;}
  180. i_val+=month.length;
  181. } else if (token=="dd"||token=="d") {
  182. date=_getInt(val,i_val,token.length,2);
  183. if(date==null||(date<1)||(date>31)){return 0;}
  184. i_val+=date.length;
  185. } else if (token=="hh"||token=="h") {
  186. hh=_getInt(val,i_val,token.length,2);
  187. if(hh==null||(hh<1)||(hh>12)){return 0;}
  188. i_val+=hh.length;
  189. } else if (token=="HH"||token=="H") {
  190. hh=_getInt(val,i_val,token.length,2);
  191. if(hh==null||(hh<0)||(hh>23)){return 0;}
  192. i_val+=hh.length;}
  193. else if (token=="KK"||token=="K") {
  194. hh=_getInt(val,i_val,token.length,2);
  195. if(hh==null||(hh<0)||(hh>11)){return 0;}
  196. i_val+=hh.length;
  197. } else if (token=="kk"||token=="k") {
  198. hh=_getInt(val,i_val,token.length,2);
  199. if(hh==null||(hh<1)||(hh>24)){return 0;}
  200. i_val+=hh.length;hh--;
  201. } else if (token=="mm"||token=="m") {
  202. mm=_getInt(val,i_val,token.length,2);
  203. if(mm==null||(mm<0)||(mm>59)){return 0;}
  204. i_val+=mm.length;
  205. } else if (token=="ss"||token=="s") {
  206. ss=_getInt(val,i_val,token.length,2);
  207. if(ss==null||(ss<0)||(ss>59)){return 0;}
  208. i_val+=ss.length;
  209. } else if (token=="a") {
  210. if (val.substring(i_val,i_val+2).toLowerCase()=="am") {ampm="AM";}
  211. else if (val.substring(i_val,i_val+2).toLowerCase()=="pm") {ampm="PM";}
  212. else {return 0;}
  213. i_val+=2;
  214. } else {
  215. if (val.substring(i_val,i_val+token.length)!=token) {return 0;}
  216. else {i_val+=token.length;}
  217. }
  218. }
  219. // If there are any trailing characters left in the value, it doesn't match
  220. if (i_val != val.length) { return 0; }
  221. // Is date valid for month?
  222. if (month==2) {
  223. // Check for leap year
  224. if ( ( (year%4==0)&&(year%100 != 0) ) || (year%400==0) ) { // leap year
  225. if (date > 29){ return 0; }
  226. } else { if (date > 28) { return 0; } }
  227. }
  228. if ((month==4)||(month==6)||(month==9)||(month==11)) {
  229. if (date > 30) { return 0; }
  230. }
  231. // Correct hours value
  232. if (hh<12 && ampm=="PM") { hh=hh-0+12; }
  233. else if (hh>11 && ampm=="AM") { hh-=12; }
  234. return new Date(year,month-1,date,hh,mm,ss);
  235. }
  236. Date.prototype.formatDate = function(dateFmt) {
  237. return formatDate(this, dateFmt);
  238. };
  239. String.prototype.parseDate = function(dateFmt) {
  240. if (this.length < dateFmt.length) {
  241. dateFmt = dateFmt.slice(0,this.length);
  242. }
  243. return parseDate(this, dateFmt);
  244. };
  245. /**
  246. * replaceTmEval("{1+2}-{2-1}")
  247. */
  248. function replaceTmEval(data){
  249. return data.replace(RegExp("({[A-Za-z0-9_+-]*})","g"), function($1){
  250. return eval('(' + $1.replace(/[{}]+/g, "") + ')');
  251. });
  252. }
  253. /**
  254. * dateFmt:%y-%M-%d
  255. * %y-%M-{%d+1}
  256. * ex: new Date().formatDateTm('%y-%M-{%d-1}')
  257. * new Date().formatDateTm('2012-1')
  258. */
  259. Date.prototype.formatDateTm = function(dateFmt) {
  260. var y = this.getFullYear();
  261. var m = this.getMonth()+1;
  262. var d = this.getDate();
  263. var sDate = dateFmt.replaceAll("%y",y).replaceAll("%M",m).replaceAll("%d",d);
  264. sDate = replaceTmEval(sDate);
  265. var _y=1900, _m=0, _d=1;
  266. var aDate = sDate.split('-');
  267. if (aDate.length > 0) _y = aDate[0];
  268. if (aDate.length > 1) _m = aDate[1]-1;
  269. if (aDate.length > 2) _d = aDate[2];
  270. return new Date(_y,_m,_d).formatDate('yyyy-MM-dd');
  271. };
  272. })();