Keine Beschreibung

dateformat.js 5.1KB

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