-
Notifications
You must be signed in to change notification settings - Fork 4
/
datef.js
324 lines (297 loc) · 10 KB
/
datef.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define([], function () {
return factory(root);
});
} else if (typeof exports === 'object') {
module.exports = factory(root);
} else {
root.datef = factory(root);
}
}(this, function (root) {
'use strict';
/**
* Converts `value` to string and pad it with leading zeroes
* until resulting string reaches `length`
*
* @param {Number} value
* @param {Number} [length=2]
*
* @return {String}
*/
function leadingZeroes(value, length) {
var str = value.toString(),
finalLen = arguments.length === 2 ? length : 2;
if (str.length > finalLen) {
return str;
}
// this task can be accomplished in one line — empty for cycle
for (str; str.length < finalLen; str = '0' + str);
return str;
}
var hasModule = !!(typeof module !== 'undefined' && module.exports);
var tokens = {
YYYY: function (date) {
return date.getFullYear();
},
YY: function (date) {
return leadingZeroes(date.getFullYear() % 100);
},
MMMM: function (date, format) {
return languages[datef.lang()].months(date, format);
},
MMM: function (date, format) {
return languages[datef.lang()].monthsShort(date, format);
},
MM: function (date) {
return leadingZeroes(date.getMonth() + 1);
},
M: function (date) {
return date.getMonth() + 1;
},
DDD: function (date) {
return languages[datef.lang()].weekdays[date.getDay()];
},
DD: function (date) {
return languages[datef.lang()].weekdaysShort[date.getDay()];
},
D: function (date) {
return languages[datef.lang()].weekdaysMin[date.getDay()];
},
dd: function (date) {
return leadingZeroes(date.getDate());
},
d: function (date) {
return date.getDate();
},
HH: function (date) {
return leadingZeroes(date.getHours());
},
H: function (date) {
return date.getHours();
},
hh: function (date) {
return leadingZeroes(date.getHours() % 12 || 12);
},
h: function (date) {
return date.getHours() % 12 || 12;
},
mm: function (date) {
return leadingZeroes(date.getMinutes());
},
m: function (date) {
return date.getMinutes();
},
ss: function (date) {
return leadingZeroes(date.getSeconds());
},
s: function (date) {
return date.getSeconds();
},
ff: function (date) {
return leadingZeroes(date.getMilliseconds(), 3);
},
f: function (date) {
return date.getMilliseconds();
},
A: function (date) {
return languages[datef.lang()].meridiem(date.getHours(), false);
},
a: function (date) {
return languages[datef.lang()].meridiem(date.getHours(), true);
},
ZZ: function (date, format, separator) {
var tz = date.getTimezoneOffset(),
hours = Math.abs(Math.floor(tz / 60)),
mins = tz % 60,
sign = tz > 0 ? '-' : '+';
separator = separator || '';
return sign + [leadingZeroes(hours), leadingZeroes(mins)].join(separator);
},
Z: function (date) {
return tokens.ZZ(date, null, ':');
}
};
var possibleFormats = [];
for (var extractor in tokens) {
if (tokens.hasOwnProperty(extractor)) {
possibleFormats.push(extractor);
}
}
var regexp = new RegExp(possibleFormats.join('|'), 'mg');
/**
* Formats date according to `format` string.
* Format string may consist of any characters, but some of them considered tokens,
* and will be replaced by appropriate value from `date`.
* Possible tokens include:
* * **YYYY**: 4-digit year
* * **YY**: last 2 digit of year
* * **MMMM**: full name of month
* * **MMM**: short name of month
* * **MM**: ISO8601-compatible number of month (i.e. zero-padded) in year (with January being 1st month)
* * **M**: number of month in year without zero-padding (with January being 1st month)
* * **DDD**: full name of day
* * **DD**: short name of day
* * **D**: min name of day
* * **dd**: zero-padded number of day in month
* * **d**: number of day in month
* * **HH**: zero-padded hour in 24-hr format
* * **H**: hour in 24-hr format
* * **hh**: zero-padded hour in 12-hr format
* * **h**: hour in 12-hr format
* * **mm**: zero-padded minutes
* * **m**: minutes
* * **ss**: zero-padded seconds
* * **s**: seconds
* * **ff**: zero-padded milliseconds
* * **f**: milliseconds
* * **A**: AM/PM
* * **a**: am/pm
* * **ZZ**: time-zone in ISO8601-compatible basic format (i.e. "-0400")
* * **Z**: time-zone in ISO8601-compatible extended format (i.e. "-04:00")
*
* Longer tokens take precedence over shorter ones (so "MM" will aways be "04", not "44" in april).
*
* @param {String} format
* @param {Date|Number|String} [date=new Date()]
*
* @return {String}
*/
function datef (format, date) {
if (!format || typeof format !== 'string') {
throw new TypeError('Argument `format` must be a string');
}
if (date !== undefined && !(date instanceof Date) && typeof date !== 'number' && typeof date !== 'string') {
throw new TypeError('Argument `date` must be instance of Date or Unix Timestamp or ISODate String');
}
var dt = (arguments.length === 2 && date) ? date instanceof Date ? date : new Date(date) : new Date();
format = format.toString();
if (datef._formatters[format]) {
return datef._formatters[format](dt);
} else {
return format.replace(regexp, function (match) {
return tokens[match](dt, format);
});
}
}
/**
* Predefined languages storage.
*
* @type {Object}
*/
var languages = datef._languages = {};
/**
* Creates lang function.
*
* @param {String} lang Language to set
* @param {Object} [options] Language options
*
* @return {String} Current language.
*/
var lang = datef.lang = function (lang, options) {
if (!lang) {
return languages.current;
}
if (!options) {
if (languages[lang] && languages.current !== lang) {
languages.current = lang;
} else if (hasModule) {
try {
require('./lang/' + lang);
languages.current = lang;
} catch (e) {
return languages.current;
}
}
return languages.current;
}
languages[lang] = options;
languages.current = lang;
return languages.current;
};
// Let's create English language
lang('en', {
_months: 'January_February_March_April_May_June_July_August_September_October_November_December'.split('_'),
months: function (date) {
return this._months[date.getMonth()];
},
_monthsShort: 'Jan_Feb_Mar_Apr_May_Jun_Jul_Aug_Sep_Oct_Nov_Dec'.split('_'),
monthsShort: function (date) {
return this._monthsShort[date.getMonth()];
},
weekdays: 'Sunday_Monday_Tuesday_Wednesday_Thursday_Friday_Saturday'.split('_'),
weekdaysShort: 'Sun_Mon_Tue_Wed_Thu_Fri_Sat'.split('_'),
weekdaysMin: 'Su_Mo_Tu_We_Th_Fr_Sa'.split('_'),
meridiem : function (hours, isLower) {
if (hours > 11) {
return isLower ? 'pm' : 'PM';
} else {
return isLower ? 'am' : 'AM';
}
}
});
/**
* Creates formatting function. Basically just curry over datef.
*
* @return {Function} Readied formatting function with one argument — date.
*/
var createFormatter = function (format) {
return function (date) {
var f = format[datef.lang()] || format['default'] || format;
return datef(f, date);
};
};
/**
* Predefined formatters storage.
*
* @type {Object}
*/
var formatters = datef._formatters = {};
/**
* Creates formatting function and files it under `datef.formatters[name]`
* Using is just `datef('myformat')`
*
* @param {String} name
* @param {String|Object} format
*
* @example
* ```js
* datef.register('longDate', 'd MMMM');
* datef.register('longDateAndTime', {
* 'en': 'MMMM d, h:mma',
* 'default': 'd MMMM, HH:mm'
* });
* ```
*
* @return {Function} Readied formatting function with one argument — date.
*/
var register = datef.register = function (name, format) {
formatters[name] = createFormatter(format);
return formatters[name];
};
/**
* Return list of custom formats
*
* @return {Array}
*/
datef.formatters = function () {
return Object.keys(datef._formatters);
};
// Let's create some basic formats
register('ISODate', 'YYYY-MM-dd');
register('ISOTime', 'hh:mm:ss');
register('ISODateTime', 'YYYY-MM-ddThh:mm:ss');
register('ISODateTimeTZ', 'YYYY-MM-ddThh:mm:ssZ');
// conflict management — save link to previous content of datef, whatever it was.
var prevDatef = root.datef;
/**
* Cleans global namespace, restoring previous value of window.datef, and returns datef itself.
*
* @return {datef}
*/
datef.noConflict = function () {
root.datef = prevDatef;
return this;
};
return datef;
}));