-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdateFormat.js
More file actions
144 lines (141 loc) · 6.78 KB
/
Copy pathdateFormat.js
File metadata and controls
144 lines (141 loc) · 6.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
function dateFormat(dt, str, showFormatted) {
function addHours(d, v) { d.setHours(d.getHours() + parseFloat(v)); return d; }
function getDayName(d, sf) { var days = [ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ]; return sf ? days[d.getDay()].substr(0,3) : days[d.getDay()]; }
function getWeek(d) { var a = new Date(d.getFullYear(), 0, 1); return Math.ceil(((d - a) / 864E5 + a.getDay() + 1) / 7); }
function getMonthName(d, shortForm) {
var months = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ];
return shortForm ? months[d.getMonth()].substr(0,3) : months[d.getMonth()];
}
var formats = {
/* DAY */
'd': function() { var a = this.getDate(); return a > 9 ? a : '0' + a; },
'D': function() { return getDayName(this, true); },
'j': function() { return this.getDate(); },
'l': function() { return getDayName(this); },
'N': function() { return this.getDay() + 1; },
'S': function() { var a = this.getDate(); return /^1[0-9]$/.test(a) ? "th" : /1$/.test(a) ? "st" : /2$/.test(a) ? "nd" : /3$/.test(a) ? "rd" : "th"; },
'w': function() { return this.getDay(); },
'z': function() { return Math.round(Math.abs((this.getTime() - new Date('1/1/' + this.getFullYear()).getTime())/(8.64e7))); },
/* WEEK */
'W': function() { return getWeek(this); },
/* MONTH */
'F': function() { return getMonthName(this); },
'm': function() { var a = this.getMonth() + 1; return a > 9 ? a : '0' + a; },
'M': function() { return getMonthName(this, true); },
'n': function() { return this.getMonth() + 1; },
't': function() { return new Date(this.getFullYear(), this.getMonth()+1, 0).getDate() },
/* YEAR */
'L': function() { var a = this.getFullYear(); return 0 == a % 4 && 0 != a % 100 || 0 == a % 400; },
'o': function() { return parseInt(this.getFullYear()); }, // todo: base on week's parent year
'Y': function() { return parseInt(this.getFullYear()); },
'y': function() { return parseInt((this.getFullYear()+'').substr(-2)); },
/* TIME */
'a': function() { return this.getHours() >= 12 ? "pm" : "am"; },
'A': function() { return this.getHours() >= 12 ? "PM" : "AM"; },
'B': function() { return "@"+("00"+Math.floor((((this.getHours()+1)%24*60+this.getMinutes())*60+this.getSeconds()+(this.getMilliseconds()*0.001))/86.4)).slice(-3); },
'g': function() { var a = this.getHours(); return a == 0 ? 12 : a <= 12 ? a : a - 12; }, // 12-hour format of an hour without leading zeros
'G': function() { return this.getHours(); }, // 24-hour format of an hour without leading zeros
'h': function() { var a = this.getHours(); a = a <= 12 ? a : a - 12; return a == 0 ? 12 : a > 9 ? a : '0' + a; }, // 12-hour format of an hour with leading zeros
'H': function() { var a = this.getHours(); return a > 9 ? a : '0' + a; }, // 24-hour format of an hour with leading zeros
'i': function() { var a = this.getMinutes(); return a > 9 ? a : '0' + a; }, // Minutes with leading zeros
's': function() { var a = this.getSeconds(); return a > 9 ? a : '0' + a; }, // Seconds, with leading zeros
'u': function() { return this.getMilliseconds(); }, // this is NOT microseconds ... it's JS :P,
/* TIMEZONE */
'e': function() { var a = this.toString().match(/ ([A-Z]{3,4})([-|+]?\d{4})/); return a.length > 1 ? a[1] : ''; },
'I': function() {
var a = new Date(this.getFullYear(), 0, 1),
b = new Date(this.getFullYear(), 6, 1),
c = Math.max(a.getTimezoneOffset(), b.getTimezoneOffset());
return this.getTimezoneOffset() < c ? 1 : 0;
},
'O': function() { var a = this.toString().match(/ ([A-Z]{3,4})([-|+]?\d{4})/); return a.length > 2 ? a[2] : ''; },
'P': function() { var a = this.toString().match(/ ([A-Z]{3,4})([-|+]?\d{4})/); return a.length > 2 ? a[2].substr(0,3) + ':' + a[2].substr(3,2) : ''; },
'T': function() { return this.toLocaleString('en', {timeZoneName:'short'}).split(' ').pop(); }, // may not be reliable on Apple Systems // NOTE: Apple Sux
'Z': function() { return this.getTimezoneOffset() * 60; },
/* FULL DATE/TIME */
'c': function() { return addHours(new Date(this), -(this.getTimezoneOffset() / 60)).toISOString(); },
'r': function() { return addHours(new Date(this), -(this.getTimezoneOffset() / 60)).toISOString(); },
'U': function() { return this.getTime() / 1000 | 0; }
},
compound = {
'commonLogFormat': 'd/M/Y:G:i:s',
'exif': 'Y:m:d G:i:s',
'isoYearWeek': 'Y\\WW',
'isoYearWeek2': 'Y-\\WW',
'isoYearWeekDay': 'Y\\WWj',
'isoYearWeekDay2': 'Y-\\WW-j',
'mySQL': 'Y-m-d h:i:s',
'postgreSQL': 'Y.z',
'postgreSQL2': 'Yz',
'soap': 'Y-m-d\\TH:i:s.u',
'soap2': 'Y-m-d\\TH:i:s.uP',
'unixTimestamp': '@U',
'xmlrpc': 'Ymd\\TG:i:s',
'xmlrpcCompact': 'Ymd\\tGis',
'wddx': 'Y-n-j\\TG:i:s'
},
constants = {
'AMERICAN': 'F j, Y',
'AMERICANSHORT': 'm/d/Y',
'AMERICANSHORTWTIME': 'm/d/Y h:i:sA',
'ATOM': 'Y-m-d\\TH:i:sP',
'COOKIE': 'l, d-M-Y H:i:s T',
'EUROPEAN': 'j F Y',
'EUROPEANSHORT': 'd.m.Y',
'EUROPEANSHORTWTIME': 'd.m.Y H:i:s',
'ISO8601': 'Y-m-d\\TH:i:sO',
'LEGAL': 'j F Y',
'RFC822': 'D, d M y H:i:s O',
'RFC850': 'l, d-M-y H:i:s T',
'RFC1036': 'D, d M y H:i:s O',
'RFC1123': 'D, d M Y H:i:s O',
'RFC2822': 'D, d M Y H:i:s O',
'RFC3339': 'Y-m-d\\TH:i:sP',
'RSS': 'D, d M Y H:i:s O',
'W3C': 'Y-m-d\\TH:i:sP'
},
pretty = {
'pretty-a': 'g:i.sA l jS \\o\\f F, Y',
'pretty-b': 'g:iA l jS \\o\\f F, Y',
'pretty-c': 'n/d/Y g:iA',
'pretty-d': 'n/d/Y',
'pretty-e': 'F jS - g:ia',
'pretty-f': 'g:iA',
'pretty-g': 'F jS, Y',
'pretty-h': 'F jS, Y g:mA'
}
if (str) {
if (str == 'compound') {
if (showFormatted === false) return compound;
var r = {};
for (var x in compound) r[x] = dateFormat(dt, compound[x]);
return r;
}
else if (compound[str]) return dateFormat(dt, compound[str], showFormatted);
if (str == 'constants') {
if (showFormatted === false) return constants;
var r = {};
for (var x in constants) r[x] = dateFormat(dt, constants[x]);
return r;
}
else if (constants[str]) return dateFormat(dt, constants[str], showFormatted);
if (str == 'pretty') {
if (showFormatted === false) return pretty;
var r = {};
for (var x in pretty) r[x] = dateFormat(dt, pretty[x]);
return r;
}
else if (pretty[str]) return dateFormat(dt, pretty[str], showFormatted);
var ret = str.split(''), lc = '';
for (var x in ret) {
var c = ret[x];
if ((c && /[a-z]/i.test(c)) && !(/\\/.test(lc + c))) {
var rx = new RegExp(c, 'g');
ret[x] = formats[c] ? formats[c].apply(dt) : c;
}
lc = ret[x];
}
return ret.join('').replace(/\\/g, '');
}
return dt;
}