dateformat.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /**
  2. * Date.parse with progressive enhancement for ISO 8601 <https://github.com/csnover/js-iso8601>
  3. * © 2011 Colin Snover <http://zetafleet.com>
  4. * Released under MIT license.
  5. */
  6. (function (Date, undefined) {
  7. var origParse = Date.parse, numericKeys = [ 1, 4, 5, 6, 7, 10, 11 ];
  8. Date.parse = function (date) {
  9. var timestamp, struct, minutesOffset = 0;
  10. // ES5 §15.9.4.2 states that the string should attempt to be parsed as a Date Time String Format string
  11. // before falling back to any implementation-specific date parsing, so that’s what we do, even if native
  12. // implementations could be faster
  13. // 1 YYYY 2 MM 3 DD 4 HH 5 mm 6 ss 7 msec 8 Z 9 ± 10 tzHH 11 tzmm
  14. if ((struct = /^(\d{4}|[+\-]\d{6})(?:-(\d{2})(?:-(\d{2}))?)?(?:T(\d{2}):(\d{2})(?::(\d{2})(?:\.(\d{3-6}))?)?(?:(Z)|([+\-])(\d{2})(?::(\d{2}))?)?)?$/.exec(date))) {
  15. // avoid NaN timestamps caused by “undefined” values being passed to Date.UTC
  16. for (var i = 0, k; (k = numericKeys[i]); ++i) {
  17. struct[k] = +struct[k] || 0;
  18. }
  19. // allow undefined days and months
  20. struct[2] = (+struct[2] || 1) - 1;
  21. struct[3] = +struct[3] || 1;
  22. if (struct[8] !== 'Z' && struct[9] !== undefined) {
  23. minutesOffset = struct[10] * 60 + struct[11];
  24. if (struct[9] === '+') {
  25. minutesOffset = 0 - minutesOffset;
  26. }
  27. }
  28. timestamp = Date.UTC(struct[1], struct[2], struct[3], struct[4], struct[5] + minutesOffset, struct[6], struct[7]);
  29. }
  30. else {
  31. timestamp = origParse ? origParse(date) : NaN;
  32. }
  33. return timestamp;
  34. };
  35. }(Date));
  36. /**
  37. * Date.parse with progressive enhancement for ISO 8601 <https://github.com/csnover/js-iso8601>
  38. * © 2011 Colin Snover <http://zetafleet.com>
  39. * Released under MIT license.
  40. */
  41. (function (Date, undefined) {
  42. var origParse = Date.parse, numericKeys = [ 1, 4, 5, 6, 7, 10, 11 ];
  43. Date.parse = function (date) {
  44. var timestamp, struct, minutesOffset = 0;
  45. // ES5 §15.9.4.2 states that the string should attempt to be parsed as a Date Time String Format string
  46. // before falling back to any implementation-specific date parsing, so that’s what we do, even if native
  47. // implementations could be faster
  48. // 1 YYYY 2 MM 3 DD 4 HH 5 mm 6 ss 7 msec 8 Z 9 ± 10 tzHH 11 tzmm
  49. if ((struct = /^(\d{4}|[+\-]\d{6})(?:-(\d{2})(?:-(\d{2}))?)?(?:T(\d{2}):(\d{2})(?::(\d{2})(?:\.(\d{3,6}))?)?(?:(Z)|([+\-])(\d{2})(?::(\d{2}))?)?)?$/.exec(date))) {
  50. // avoid NaN timestamps caused by “undefined” values being passed to Date.UTC
  51. for (var i = 0, k; (k = numericKeys[i]); ++i) {
  52. struct[k] = +struct[k] || 0;
  53. }
  54. // allow undefined days and months
  55. struct[2] = (+struct[2] || 1) - 1;
  56. struct[3] = +struct[3] || 1;
  57. if (struct[8] !== 'Z' && struct[9] !== undefined) {
  58. minutesOffset = struct[10] * 60 + struct[11];
  59. if (struct[9] === '+') {
  60. minutesOffset = 0 - minutesOffset;
  61. }
  62. }
  63. if(struct[7] > 999)
  64. struct[7] = parseInt(struct[7].toString().substring(0,2))
  65. timestamp = Date.UTC(struct[1], struct[2], struct[3], struct[4], struct[5] + minutesOffset, struct[6], struct[7]);
  66. }
  67. else {
  68. timestamp = origParse ? origParse(date) : NaN;
  69. }
  70. return timestamp;
  71. };
  72. }(Date));
  73. /*
  74. * Date Format 1.2.3
  75. * (c) 2007-2009 Steven Levithan <stevenlevithan.com>
  76. * MIT license
  77. *
  78. * Includes enhancements by Scott Trenda <scott.trenda.net>
  79. * and Kris Kowal <cixar.com/~kris.kowal/>
  80. *
  81. * Accepts a date, a mask, or a date and a mask.
  82. * Returns a formatted version of the given date.
  83. * The date defaults to the current date/time.
  84. * The mask defaults to dateFormat.masks.default.
  85. */
  86. var dateFormat = function () {
  87. var token = /d{1,4}|m{1,4}|yy(?:yy)?|([HhMsTt])\1?|[LloSZ]|"[^"]*"|'[^']*'/g,
  88. timezone = /\b(?:[PMCEA][SDP]T|(?:Pacific|Mountain|Central|Eastern|Atlantic) (?:Standard|Daylight|Prevailing) Time|(?:GMT|UTC)(?:[-+]\d{4})?)\b/g,
  89. timezoneClip = /[^-+\dA-Z]/g,
  90. pad = function (val, len) {
  91. val = String(val);
  92. len = len || 2;
  93. while (val.length < len) val = "0" + val;
  94. return val;
  95. };
  96. // Regexes and supporting functions are cached through closure
  97. return function (date, mask, utc) {
  98. var dF = dateFormat;
  99. // You can't provide utc if you skip other args (use the "UTC:" mask prefix)
  100. if (arguments.length == 1 && Object.prototype.toString.call(date) == "[object String]" && !/\d/.test(date)) {
  101. mask = date;
  102. date = undefined;
  103. }
  104. // Passing date through Date applies Date.parse, if necessary
  105. date = date ? new Date(date) : new Date;
  106. if (isNaN(date)) throw SyntaxError("invalid date");
  107. mask = String(dF.masks[mask] || mask || dF.masks["default"]);
  108. // Allow setting the utc argument via the mask
  109. if (mask.slice(0, 4) == "UTC:") {
  110. mask = mask.slice(4);
  111. utc = true;
  112. }
  113. var _ = utc ? "getUTC" : "get",
  114. d = date[_ + "Date"](),
  115. D = date[_ + "Day"](),
  116. m = date[_ + "Month"](),
  117. y = date[_ + "FullYear"](),
  118. H = date[_ + "Hours"](),
  119. M = date[_ + "Minutes"](),
  120. s = date[_ + "Seconds"](),
  121. L = date[_ + "Milliseconds"](),
  122. o = utc ? 0 : date.getTimezoneOffset(),
  123. flags = {
  124. d: d,
  125. dd: pad(d),
  126. ddd: dF.i18n.dayNames[D],
  127. dddd: dF.i18n.dayNames[D + 7],
  128. m: m + 1,
  129. mm: pad(m + 1),
  130. mmm: dF.i18n.monthNames[m],
  131. mmmm: dF.i18n.monthNames[m + 12],
  132. yy: String(y).slice(2),
  133. yyyy: y,
  134. h: H % 12 || 12,
  135. hh: pad(H % 12 || 12),
  136. H: H,
  137. HH: pad(H),
  138. M: M,
  139. MM: pad(M),
  140. s: s,
  141. ss: pad(s),
  142. l: pad(L, 3),
  143. L: pad(L > 99 ? Math.round(L / 10) : L),
  144. t: H < 12 ? "a" : "p",
  145. tt: H < 12 ? "am" : "pm",
  146. T: H < 12 ? "A" : "P",
  147. TT: H < 12 ? "AM" : "PM",
  148. Z: utc ? "UTC" : (String(date).match(timezone) || [""]).pop().replace(timezoneClip, ""),
  149. o: (o > 0 ? "-" : "+") + pad(Math.floor(Math.abs(o) / 60) * 100 + Math.abs(o) % 60, 4),
  150. S: ["th", "st", "nd", "rd"][d % 10 > 3 ? 0 : (d % 100 - d % 10 != 10) * d % 10]
  151. };
  152. return mask.replace(token, function ($0) {
  153. return $0 in flags ? flags[$0] : $0.slice(1, $0.length - 1);
  154. });
  155. };
  156. }();
  157. // Some common format strings
  158. dateFormat.masks = {
  159. "default": "ddd mmm dd yyyy HH:MM:ss",
  160. shortDate: "m/d/yy",
  161. mediumDate: "mmm d, yyyy",
  162. longDate: "mmmm d, yyyy",
  163. fullDate: "dddd, mmmm d, yyyy",
  164. shortTime: "h:MM TT",
  165. mediumTime: "h:MM:ss TT",
  166. longTime: "h:MM:ss TT Z",
  167. isoDate: "yyyy-mm-dd",
  168. isoTime: "HH:MM:ss",
  169. isoDateTime: "yyyy-mm-dd'T'HH:MM:ss",
  170. isoUtcDateTime: "UTC:yyyy-mm-dd'T'HH:MM:ss'Z'"
  171. };
  172. // Internationalization strings
  173. dateFormat.i18n = {
  174. dayNames: [
  175. "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
  176. "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
  177. ],
  178. monthNames: [
  179. "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
  180. "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
  181. ]
  182. };
  183. // For convenience...
  184. Date.prototype.format = function (mask, utc) {
  185. return dateFormat(this, mask, utc);
  186. };
  187. Date.prototype.add_days = function(days) {
  188. var dat = new Date(this.valueOf())
  189. dat.setDate(dat.getDate() + days);
  190. return dat;
  191. }
  192. function date_range(startDate, stopDate) {
  193. var dateArray = new Array();
  194. var currentDate = startDate;
  195. while (currentDate <= stopDate) {
  196. dateArray.push( new Date (currentDate) )
  197. currentDate = currentDate.add_days(1);
  198. }
  199. return dateArray;
  200. }