A unf. social network done poorly.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
4.5KB

  1. // Date Format 1.2.3\\
  2. // © 2007-2009 Steven Levithan ([[http://blog.stevenlevithan.com/archives/date-time-format|stevenlevithan.com]])\\
  3. // MIT license
  4. //
  5. // Includes enhancements by Scott Trenda
  6. // and Kris Kowal ([[http://cixar.com/~kris.kowal/|cixar.com/~kris.kowal/]])
  7. //
  8. // Accepts a date, a mask, or a date and a mask and returns a formatted version
  9. // of the given date.
  10. //
  11. // ==== Parameters ====
  12. // * {{{date}}} is a {{{Date()}}} object. If not specified, the date defaults to the
  13. // current date/time.
  14. // * {{{mask}}} is a string that defines the formatting of the date. Formatting
  15. // options can be found in the
  16. // [[http://blog.stevenlevithan.com/archives/date-time-format|Date Format]]
  17. // documentation. If not specified, the mask defaults to {{{dateFormat.masks.default}}}.
  18. var dateFormat = function() {
  19. var token = /d{1,4}|m{1,4}|yy(?:yy)?|([HhMsTt])\1?|[LloSZ]|"[^"]*"|'[^']*'/g,
  20. timezone = new RegExp('\b(?:[PMCEA][SDP]T|(?:Pacific|Mountain|Central|Eastern|Atlantic) ' +
  21. '(?:Standard|Daylight|Prevailing) Time|(?:GMT|UTC)(?:[-+]\d{4})?)\b',
  22. 'g'),
  23. timezoneClip = /[^-+\dA-Z]/g,
  24. pad = function (val, len) {
  25. val = String(val);
  26. len = len || 2;
  27. while (val.length < len) val = "0" + val;
  28. return val;
  29. };
  30. // Regexes and supporting functions are cached through closure
  31. return function (date, mask, utc) {
  32. // You can't provide utc if you skip other args (use the "UTC:" mask prefix)
  33. if (arguments.length == 1 && Object.prototype.toString.call(date) ==
  34. "[object String]" && !/\d/.test(date)) {
  35. mask = date;
  36. date = undefined;
  37. }
  38. // Passing date through Date applies Date.parse, if necessary
  39. date = date ? new Date(date) : new Date;
  40. if (isNaN(date)) throw SyntaxError("invalid date");
  41. mask = String(dateFormat.masks[mask] ||
  42. mask ||
  43. dateFormat.masks["default"]);
  44. // Allow setting the utc argument via the mask
  45. if (mask.slice(0, 4) == "UTC:") {
  46. mask = mask.slice(4);
  47. utc = true;
  48. }
  49. var _ = utc ? "getUTC" : "get",
  50. d = date[_ + "Date"](),
  51. D = date[_ + "Day"](),
  52. m = date[_ + "Month"](),
  53. y = date[_ + "FullYear"](),
  54. H = date[_ + "Hours"](),
  55. M = date[_ + "Minutes"](),
  56. s = date[_ + "Seconds"](),
  57. L = date[_ + "Milliseconds"](),
  58. o = utc ? 0 : date.getTimezoneOffset(),
  59. flags = {
  60. d: d,
  61. dd: pad(d),
  62. ddd: AjaxIM.l10n.dayNames[D],
  63. dddd: AjaxIM.l10n.dayNames[D + 7],
  64. m: m + 1,
  65. mm: pad(m + 1),
  66. mmm: AjaxIM.l10n.monthNames[m],
  67. mmmm: AjaxIM.l10n.monthNames[m + 12],
  68. yy: String(y).slice(2),
  69. yyyy: y,
  70. h: H % 12 || 12,
  71. hh: pad(H % 12 || 12),
  72. H: H,
  73. HH: pad(H),
  74. M: M,
  75. MM: pad(M),
  76. s: s,
  77. ss: pad(s),
  78. l: pad(L, 3),
  79. L: pad(L > 99 ? Math.round(L / 10) : L),
  80. t: H < 12 ? "a" : "p",
  81. tt: H < 12 ? "am" : "pm",
  82. T: H < 12 ? "A" : "P",
  83. TT: H < 12 ? "AM" : "PM",
  84. Z: utc ? "UTC" :
  85. (String(date).match(timezone) || [""])
  86. .pop().replace(timezoneClip, ""),
  87. o: (o > 0 ? "-" : "+") +
  88. pad(Math.floor(Math.abs(o) / 60) * 100 + Math.abs(o) % 60, 4),
  89. S: ["th", "st", "nd", "rd"][d % 10 > 3 ?
  90. 0 :
  91. (d % 100 - d % 10 != 10) * d % 10]
  92. };
  93. return mask.replace(token, function ($0) {
  94. return $0 in flags ? flags[$0] : $0.slice(1, $0.length - 1);
  95. });
  96. };
  97. }();
  98. // Some common format strings
  99. dateFormat.masks = {
  100. "default": "ddd mmm dd yyyy HH:MM:ss",
  101. shortDate: "m/d/yy",
  102. mediumDate: "mmm d, yyyy",
  103. longDate: "mmmm d, yyyy",
  104. fullDate: "dddd, mmmm d, yyyy",
  105. shortTime: "h:MM TT",
  106. mediumTime: "h:MM:ss TT",
  107. longTime: "h:MM:ss TT Z",
  108. isoDate: "yyyy-mm-dd",
  109. isoTime: "HH:MM:ss",
  110. isoDateTime: "yyyy-mm-dd'T'HH:MM:ss",
  111. isoUtcDateTime: "UTC:yyyy-mm-dd'T'HH:MM:ss'Z'"
  112. };