-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathjquery.formatDateTime.js
271 lines (253 loc) · 10.6 KB
/
jquery.formatDateTime.js
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
* jQuery formatDateTime Plugin
*
* Copyright 2012 Adam Gschwender
* Dual licensed under the MIT or GPL Version 2 licenses.
*/
;(function (factory) {
if (typeof exports === 'object') {
// Node/CommonJS style for Browserify
module.exports = factory;
} else if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['jquery'], factory);
} else {
// Browser globals: jQuery or jQuery-like library, such as Zepto
factory(window.jQuery || window.$);
}
}(function($) {
var ticksTo1970 = (((1970 - 1) * 365 + Math.floor(1970 / 4)
- Math.floor(1970 / 100)
+ Math.floor(1970 / 400)) * 24 * 60 * 60 * 10000000);
var formatDateTime = function(format, date, settings) {
var output = '';
var literal = false;
var iFormat = 0;
// Check whether a format character is doubled
var lookAhead = function(match) {
var matches = (iFormat + 1 < format.length
&& format.charAt(iFormat + 1) == match);
if (matches) {
iFormat++;
}
return matches;
};
// Format a number, with leading zero if necessary
var formatNumber = function(match, value, len) {
var num = '' + value;
if (lookAhead(match)) {
while (num.length < len) {
num = '0' + num;
}
}
return num;
};
// Format a name, short or long as requested
var formatName = function(match, value, shortNames, longNames) {
return (lookAhead(match) ? longNames[value] : shortNames[value]);
};
// Get the value for the supplied unit, e.g. year for y
var getUnitValue = function(unit) {
var utc = settings.utc;
switch (unit) {
case 'y': return utc ? date.getUTCFullYear() : date.getFullYear();
case 'm': return (utc ? date.getUTCMonth() : date.getMonth()) + 1;
case 'M': return utc ? date.getUTCMonth() : date.getMonth();
case 'd': return utc ? date.getUTCDate() : date.getDate();
case 'D': return utc ? date.getUTCDay() : date.getDay();
case 'g':
return (utc ? date.getUTCHours() : date.getHours()) % 12 || 12;
case 'h': return utc ? date.getUTCHours() : date.getHours();
case 'i': return utc ? date.getUTCMinutes() : date.getMinutes();
case 's': return utc ? date.getUTCSeconds() : date.getSeconds();
case 'u':
return utc ? date.getUTCMilliseconds() : date.getMilliseconds();
default: return '';
}
};
for (iFormat = 0; iFormat < format.length; iFormat++) {
if (literal) {
if (format.charAt(iFormat) == "'" && !lookAhead("'")) {
literal = false;
}
else {
output += format.charAt(iFormat);
}
} else {
switch (format.charAt(iFormat)) {
case 'a':
output += getUnitValue('h') < 12
? settings.ampmNames[0]
: settings.ampmNames[1];
break;
case 'd':
output += formatNumber('d', getUnitValue('d'), 2);
break;
case 'S':
var v = getUnitValue(iFormat && format.charAt(iFormat-1));
output += (v && (settings.getSuffix || $.noop)(v)) || '';
break;
case 'D':
output += formatName('D',
getUnitValue('D'),
settings.dayNamesShort,
settings.dayNames);
break;
case 'o':
var end = new Date(date.getFullYear(),
date.getMonth(),
date.getDate()).getTime();
var start = new Date(date.getFullYear(), 0, 0).getTime();
output += formatNumber(
'o', Math.round((end - start) / 86400000), 3);
break;
case 'g':
output += formatNumber('g', getUnitValue('g'), 2);
break;
case 'h':
output += formatNumber('h', getUnitValue('h'), 2);
break;
case 'u':
output += formatNumber('u', getUnitValue('u'), 3);
break;
case 'i':
output += formatNumber('i', getUnitValue('i'), 2);
break;
case 'm':
output += formatNumber('m', getUnitValue('m'), 2);
break;
case 'M':
output += formatName('M',
getUnitValue('M'),
settings.monthNamesShort,
settings.monthNames);
break;
case 's':
output += formatNumber('s', getUnitValue('s'), 2);
break;
case 'y':
output += (lookAhead('y')
? getUnitValue('y')
: ('' + getUnitValue('y')).substr(2));
break;
case '@':
output += date.getTime();
break;
case '!':
output += date.getTime() * 10000 + ticksTo1970;
break;
case "'":
if (lookAhead("'")) {
output += "'";
} else {
literal = true;
}
break;
default:
output += format.charAt(iFormat);
}
}
}
return output;
};
$.fn.formatDateTime = function(format, settings) {
settings = $.extend({}, $.formatDateTime.defaults, settings);
this.each(function() {
var date = $(this).attr(settings.attribute);
// Use explicit format string first,
// then fallback to format attribute
var fmt = format || $(this).attr(settings.formatAttribute);
if (typeof date === 'undefined' || date === false) {
date = $(this).text();
}
if (date === '') {
$(this).text('');
} else {
$(this).text(formatDateTime(fmt, new Date(date), settings));
}
});
return this;
};
/**
Format a date object into a string value.
The format can be combinations of the following:
a - Ante meridiem and post meridiem
d - day of month (no leading zero)
dd - day of month (two digit)
o - day of year (no leading zeros)
oo - day of year (three digit)
D - day name short
DD - day name long
g - 12-hour hour format of day (no leading zero)
gg - 12-hour hour format of day (two digit)
h - 24-hour hour format of day (no leading zero)
hh - 24-hour hour format of day (two digit)
u - millisecond of second (no leading zeros)
uu - millisecond of second (three digit)
i - minute of hour (no leading zero)
ii - minute of hour (two digit)
m - month of year (no leading zero)
mm - month of year (two digit)
M - month name short
MM - month name long
S - ordinal suffix for the previous unit
s - second of minute (no leading zero)
ss - second of minute (two digit)
y - year (two digit)
yy - year (four digit)
@ - Unix timestamp (ms since 01/01/1970)
! - Windows ticks (100ns since 01/01/0001)
'...' - literal text
'' - single quote
@param format string - the desired format of the date
@param date Date - the date value to format
@param settings Object - attributes include:
ampmNames string[2] - am/pm (optional)
dayNamesShort string[7] - abbreviated names of the days
from Sunday (optional)
dayNames string[7] - names of the days from Sunday (optional)
monthNamesShort string[12] - abbreviated names of the months
(optional)
monthNames string[12] - names of the months (optional)
getSuffix function(num) - accepts a number and returns
its suffix
attribute string - Attribute which stores datetime, defaults
to data-datetime, only valid when called
on dom element(s). If not present,
uses text.
formatAttribute string - Attribute which stores the format, defaults
to data-dateformat.
utc bool - render dates using UTC instead of local time
@return string - the date in the above format
*/
$.formatDateTime = function(format, date, settings) {
settings = $.extend({}, $.formatDateTime.defaults, settings);
if (!date) { return ''; }
return formatDateTime(format, date, settings);
};
$.formatDateTime.defaults = {
monthNames: ['January','February','March','April','May','June',
'July','August','September','October','November',
'December'],
monthNamesShort: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul',
'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
dayNames: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday',
'Friday', 'Saturday'],
dayNamesShort: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
ampmNames: ['AM', 'PM'],
getSuffix: function (num) {
if (num > 3 && num < 21) {
return 'th';
}
switch (num % 10) {
case 1: return "st";
case 2: return "nd";
case 3: return "rd";
default: return "th";
}
},
attribute: 'data-datetime',
formatAttribute: 'data-dateformat',
utc: false
};
}));