dateFormat.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. !function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.dateformat=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
  2. /*
  3. * Date Format 1.2.3
  4. * (c) 2007-2009 Steven Levithan <stevenlevithan.com>
  5. * MIT license
  6. *
  7. * Includes enhancements by Scott Trenda <scott.trenda.net>
  8. * and Kris Kowal <cixar.com/~kris.kowal/>
  9. *
  10. * Accepts a date, a mask, or a date and a mask.
  11. * Returns a formatted version of the given date.
  12. * The date defaults to the current date/time.
  13. * The mask defaults to dateFormat.masks.default.
  14. */
  15. var dateFormat = function () {
  16. var token = /d{1,4}|m{1,4}|yy(?:yy)?|([HhMsTt])\1?|[LloSZWN]|"[^"]*"|'[^']*'/g,
  17. timezone = /\b(?:[PMCEA][SDP]T|(?:Pacific|Mountain|Central|Eastern|Atlantic) (?:Standard|Daylight|Prevailing) Time|(?:GMT|UTC)(?:[-+]\d{4})?)\b/g,
  18. timezoneClip = /[^-+\dA-Z]/g,
  19. pad = function (val, len) {
  20. val = String(val);
  21. len = len || 2;
  22. while (val.length < len) val = "0" + val;
  23. return val;
  24. },
  25. /**
  26. * Get the ISO 8601 week number
  27. * Based on comments from
  28. * http://techblog.procurios.nl/k/n618/news/view/33796/14863/Calculate-ISO-8601-week-and-year-in-javascript.html
  29. */
  30. getWeek = function (date) {
  31. // Remove time components of date
  32. var targetThursday = new Date(date.getFullYear(), date.getMonth(), date.getDate());
  33. // Change date to Thursday same week
  34. targetThursday.setDate(targetThursday.getDate() - ((targetThursday.getDay() + 6) % 7) + 3);
  35. // Take January 4th as it is always in week 1 (see ISO 8601)
  36. var firstThursday = new Date(targetThursday.getFullYear(), 0, 4);
  37. // Change date to Thursday same week
  38. firstThursday.setDate(firstThursday.getDate() - ((firstThursday.getDay() + 6) % 7) + 3);
  39. // Check if daylight-saving-time-switch occured and correct for it
  40. var ds = targetThursday.getTimezoneOffset() - firstThursday.getTimezoneOffset();
  41. targetThursday.setHours(targetThursday.getHours() - ds);
  42. // Number of weeks between target Thursday and first Thursday
  43. var weekDiff = (targetThursday - firstThursday) / (86400000*7);
  44. return 1 + Math.floor(weekDiff);
  45. },
  46. /**
  47. * Get ISO-8601 numeric representation of the day of the week
  48. * 1 (for Monday) through 7 (for Sunday)
  49. */
  50. getDayOfWeek = function(date){
  51. var dow = date.getDay();
  52. if(dow === 0) dow = 7;
  53. return dow;
  54. };
  55. // Regexes and supporting functions are cached through closure
  56. return function (date, mask, utc, gmt) {
  57. var dF = dateFormat;
  58. // You can't provide utc if you skip other args (use the "UTC:" mask prefix)
  59. if (arguments.length == 1 && Object.prototype.toString.call(date) == "[object String]" && !/\d/.test(date)) {
  60. mask = date;
  61. date = undefined;
  62. }
  63. date = date || new Date;
  64. if(!(date instanceof Date)) {
  65. date = new Date(date);
  66. }
  67. if (isNaN(date)) {
  68. throw TypeError("Invalid date");
  69. }
  70. mask = String(dF.masks[mask] || mask || dF.masks["default"]);
  71. // Allow setting the utc/gmt argument via the mask
  72. var maskSlice = mask.slice(0, 4);
  73. if (maskSlice == "UTC:" || maskSlice == "GMT:") {
  74. mask = mask.slice(4);
  75. utc = true;
  76. if (maskSlice == "GMT:") {
  77. gmt = true;
  78. }
  79. }
  80. var _ = utc ? "getUTC" : "get",
  81. d = date[_ + "Date"](),
  82. D = date[_ + "Day"](),
  83. m = date[_ + "Month"](),
  84. y = date[_ + "FullYear"](),
  85. H = date[_ + "Hours"](),
  86. M = date[_ + "Minutes"](),
  87. s = date[_ + "Seconds"](),
  88. L = date[_ + "Milliseconds"](),
  89. o = utc ? 0 : date.getTimezoneOffset(),
  90. W = getWeek(date),
  91. N = getDayOfWeek(date),
  92. flags = {
  93. d: d,
  94. dd: pad(d),
  95. ddd: dF.i18n.dayNames[D],
  96. dddd: dF.i18n.dayNames[D + 7],
  97. m: m + 1,
  98. mm: pad(m + 1),
  99. mmm: dF.i18n.monthNames[m],
  100. mmmm: dF.i18n.monthNames[m + 12],
  101. yy: String(y).slice(2),
  102. yyyy: y,
  103. h: H % 12 || 12,
  104. hh: pad(H % 12 || 12),
  105. H: H,
  106. HH: pad(H),
  107. M: M,
  108. MM: pad(M),
  109. s: s,
  110. ss: pad(s),
  111. l: pad(L, 3),
  112. L: pad(Math.round(L / 10)),
  113. t: H < 12 ? "a" : "p",
  114. tt: H < 12 ? "am" : "pm",
  115. T: H < 12 ? "A" : "P",
  116. TT: H < 12 ? "AM" : "PM",
  117. Z: gmt ? "GMT" : utc ? "UTC" : (String(date).match(timezone) || [""]).pop().replace(timezoneClip, ""),
  118. o: (o > 0 ? "-" : "+") + pad(Math.floor(Math.abs(o) / 60) * 100 + Math.abs(o) % 60, 4),
  119. S: ["th", "st", "nd", "rd"][d % 10 > 3 ? 0 : (d % 100 - d % 10 != 10) * d % 10],
  120. W: W,
  121. N: N
  122. };
  123. return mask.replace(token, function ($0) {
  124. return $0 in flags ? flags[$0] : $0.slice(1, $0.length - 1);
  125. });
  126. };
  127. }();
  128. // Some common format strings
  129. dateFormat.masks = {
  130. "default": "ddd mmm dd yyyy HH:MM:ss",
  131. shortDate: "m/d/yy",
  132. mediumDate: "mmm d, yyyy",
  133. longDate: "mmmm d, yyyy",
  134. fullDate: "dddd, mmmm d, yyyy",
  135. shortTime: "h:MM TT",
  136. mediumTime: "h:MM:ss TT",
  137. longTime: "h:MM:ss TT Z",
  138. isoDate: "yyyy-mm-dd",
  139. isoTime: "HH:MM:ss",
  140. isoDateTime: "yyyy-mm-dd'T'HH:MM:ss",
  141. isoUtcDateTime: "UTC:yyyy-mm-dd'T'HH:MM:ss'Z'",
  142. expiresHeaderFormat: "ddd, dd mmm yyyy HH:MM:ss Z"
  143. };
  144. // Internationalization strings
  145. dateFormat.i18n = {
  146. dayNames: [
  147. "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
  148. "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
  149. ],
  150. monthNames: [
  151. "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
  152. "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
  153. ]
  154. };
  155. /*
  156. // For convenience...
  157. Date.prototype.format = function (mask, utc) {
  158. return dateFormat(this, mask, utc);
  159. };
  160. */
  161. if (typeof exports !== "undefined") {
  162. module.exports = dateFormat;
  163. }
  164. },{}]},{},[1])(1)
  165. });