Personal website https://benkurtovic.com/
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

date_format.js 3.8 KiB

il y a 13 ans
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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?|[LloSZ]|"[^"]*"|'[^']*'/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. // Regexes and supporting functions are cached through closure
  25. return function (date, mask, utc) {
  26. var dF = dateFormat;
  27. // You can't provide utc if you skip other args (use the "UTC:" mask prefix)
  28. if (arguments.length == 1 && Object.prototype.toString.call(date) == "[object String]" && !/\d/.test(date)) {
  29. mask = date;
  30. date = undefined;
  31. }
  32. // Passing date through Date applies Date.parse, if necessary
  33. date = date ? new Date(date) : new Date;
  34. if (isNaN(date)) throw SyntaxError("invalid date");
  35. mask = String(dF.masks[mask] || mask || dF.masks["default"]);
  36. // Allow setting the utc argument via the mask
  37. if (mask.slice(0, 4) == "UTC:") {
  38. mask = mask.slice(4);
  39. utc = true;
  40. }
  41. var _ = utc ? "getUTC" : "get",
  42. d = date[_ + "Date"](),
  43. D = date[_ + "Day"](),
  44. m = date[_ + "Month"](),
  45. y = date[_ + "FullYear"](),
  46. H = date[_ + "Hours"](),
  47. M = date[_ + "Minutes"](),
  48. s = date[_ + "Seconds"](),
  49. L = date[_ + "Milliseconds"](),
  50. o = utc ? 0 : date.getTimezoneOffset(),
  51. flags = {
  52. d: d,
  53. dd: pad(d),
  54. ddd: dF.i18n.dayNames[D],
  55. dddd: dF.i18n.dayNames[D + 7],
  56. m: m + 1,
  57. mm: pad(m + 1),
  58. mmm: dF.i18n.monthNames[m],
  59. mmmm: dF.i18n.monthNames[m + 12],
  60. yy: String(y).slice(2),
  61. yyyy: y,
  62. h: H % 12 || 12,
  63. hh: pad(H % 12 || 12),
  64. H: H,
  65. HH: pad(H),
  66. M: M,
  67. MM: pad(M),
  68. s: s,
  69. ss: pad(s),
  70. l: pad(L, 3),
  71. L: pad(L > 99 ? Math.round(L / 10) : L),
  72. t: H < 12 ? "a" : "p",
  73. tt: H < 12 ? "am" : "pm",
  74. T: H < 12 ? "A" : "P",
  75. TT: H < 12 ? "AM" : "PM",
  76. Z: utc ? "UTC" : (String(date).match(timezone) || [""]).pop().replace(timezoneClip, ""),
  77. o: (o > 0 ? "-" : "+") + pad(Math.floor(Math.abs(o) / 60) * 100 + Math.abs(o) % 60, 4),
  78. S: ["th", "st", "nd", "rd"][d % 10 > 3 ? 0 : (d % 100 - d % 10 != 10) * d % 10]
  79. };
  80. return mask.replace(token, function ($0) {
  81. return $0 in flags ? flags[$0] : $0.slice(1, $0.length - 1);
  82. });
  83. };
  84. }();
  85. // Some common format strings
  86. dateFormat.masks = {
  87. "default": "ddd mmm dd yyyy HH:MM:ss",
  88. shortDate: "m/d/yy",
  89. mediumDate: "mmm d, yyyy",
  90. longDate: "mmmm d, yyyy",
  91. fullDate: "dddd, mmmm d, yyyy",
  92. shortTime: "h:MM TT",
  93. mediumTime: "h:MM:ss TT",
  94. longTime: "h:MM:ss TT Z",
  95. isoDate: "yyyy-mm-dd",
  96. isoTime: "HH:MM:ss",
  97. isoDateTime: "yyyy-mm-dd'T'HH:MM:ss",
  98. isoUtcDateTime: "UTC:yyyy-mm-dd'T'HH:MM:ss'Z'"
  99. };
  100. // Internationalization strings
  101. dateFormat.i18n = {
  102. dayNames: [
  103. "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
  104. "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
  105. ],
  106. monthNames: [
  107. "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
  108. "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
  109. ]
  110. };
  111. // For convenience...
  112. Date.prototype.format = function (mask, utc) {
  113. return dateFormat(this, mask, utc);
  114. };