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