3
* (c) 2007-2009 Steven Levithan <stevenlevithan.com>
6
* Includes enhancements by Scott Trenda <scott.trenda.net>
7
* and Kris Kowal <cixar.com/~kris.kowal/>
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.
15
var 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) {
22
while (val.length < len) val = "0" + val;
26
// Regexes and supporting functions are cached through closure
27
return function (date, mask, utc) {
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)) {
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");
40
mask = String(dF.masks[mask] || mask || dF.masks["default"]);
42
// Allow setting the utc argument via the mask
43
if (mask.slice(0, 4) == "UTC:") {
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(),
61
ddd: dF.i18n.dayNames[D],
62
dddd: dF.i18n.dayNames[D + 7],
65
mmm: dF.i18n.monthNames[m],
66
mmmm: dF.i18n.monthNames[m + 12],
67
yy: String(y).slice(2),
70
hh: pad(H % 12 || 12),
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]
88
return mask.replace(token, function ($0) {
89
return $0 in flags ? flags[$0] : $0.slice(1, $0.length - 1);
94
// Some common format strings
96
"default": "ddd mmm dd yyyy HH:MM:ss",
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",
106
isoDateTime: "yyyy-mm-dd'T'HH:MM:ss",
107
isoUtcDateTime: "UTC:yyyy-mm-dd'T'HH:MM:ss'Z'"
110
// Internationalization strings
113
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
114
"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
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"
122
// For convenience...
123
Date.prototype.format = function (mask, utc) {
124
return dateFormat(this, mask, utc);