date-and-time 2.2.0 → 2.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/PLUGINS.md +22 -22
- package/README.md +87 -83
- package/date-and-time.d.ts +304 -0
- package/date-and-time.js +76 -74
- package/date-and-time.min.js +8 -8
- package/esm/date-and-time.es.js +76 -74
- package/esm/date-and-time.es.min.js +7 -7
- package/esm/date-and-time.mjs +76 -74
- package/esm/plugin/timezone.es.js +3 -5
- package/esm/plugin/timezone.mjs +3 -5
- package/locale/ar.d.ts +1 -0
- package/locale/az.d.ts +1 -0
- package/locale/bn.d.ts +1 -0
- package/locale/cs.d.ts +1 -0
- package/locale/de.d.ts +1 -0
- package/locale/dk.d.ts +1 -0
- package/locale/el.d.ts +1 -0
- package/locale/en.d.ts +1 -0
- package/locale/es.d.ts +1 -0
- package/locale/fa.d.ts +1 -0
- package/locale/fr.d.ts +1 -0
- package/locale/hi.d.ts +1 -0
- package/locale/hu.d.ts +1 -0
- package/locale/id.d.ts +1 -0
- package/locale/it.d.ts +1 -0
- package/locale/ja.d.ts +1 -0
- package/locale/jv.d.ts +1 -0
- package/locale/ko.d.ts +1 -0
- package/locale/my.d.ts +1 -0
- package/locale/nl.d.ts +1 -0
- package/locale/pa-in.d.ts +1 -0
- package/locale/pl.d.ts +1 -0
- package/locale/pt.d.ts +1 -0
- package/locale/ro.d.ts +1 -0
- package/locale/ru.d.ts +1 -0
- package/locale/rw.d.ts +1 -0
- package/locale/sr.d.ts +1 -0
- package/locale/sv.d.ts +1 -0
- package/locale/th.d.ts +1 -0
- package/locale/tr.d.ts +1 -0
- package/locale/uk.d.ts +1 -0
- package/locale/uz.d.ts +1 -0
- package/locale/vi.d.ts +1 -0
- package/locale/zh-cn.d.ts +1 -0
- package/locale/zh-tw.d.ts +1 -0
- package/package.json +7 -4
- package/plugin/day-of-week.d.ts +1 -0
- package/plugin/meridiem.d.ts +1 -0
- package/plugin/microsecond.d.ts +1 -0
- package/plugin/ordinal.d.ts +1 -0
- package/plugin/timespan.d.ts +24 -0
- package/plugin/timezone.d.ts +79 -0
- package/plugin/timezone.js +3 -5
- package/plugin/two-digit-year.d.ts +1 -0
package/esm/date-and-time.es.js
CHANGED
|
@@ -140,9 +140,9 @@ var locales = {},
|
|
|
140
140
|
date;
|
|
141
141
|
|
|
142
142
|
/**
|
|
143
|
-
* Compiling
|
|
144
|
-
* @param {string} formatString -
|
|
145
|
-
* @returns {Array.<string>}
|
|
143
|
+
* Compiling format strings
|
|
144
|
+
* @param {string} formatString - A format string
|
|
145
|
+
* @returns {Array.<string>} A compiled object
|
|
146
146
|
*/
|
|
147
147
|
proto.compile = function (formatString) {
|
|
148
148
|
var re = /\[([^\[\]]|\[[^\[\]]*])*]|([A-Za-z])\2+|\.{3}|./g, keys, pattern = [formatString];
|
|
@@ -154,11 +154,11 @@ proto.compile = function (formatString) {
|
|
|
154
154
|
};
|
|
155
155
|
|
|
156
156
|
/**
|
|
157
|
-
* Formatting
|
|
158
|
-
* @param {Date} dateObj -
|
|
159
|
-
* @param {string|Array.<string>} arg -
|
|
160
|
-
* @param {boolean} [utc] -
|
|
161
|
-
* @returns {string}
|
|
157
|
+
* Formatting date and time objects (Date -> String)
|
|
158
|
+
* @param {Date} dateObj - A Date object
|
|
159
|
+
* @param {string|Array.<string>} arg - A format string or its compiled object
|
|
160
|
+
* @param {boolean} [utc] - Output as UTC
|
|
161
|
+
* @returns {string} A formatted string
|
|
162
162
|
*/
|
|
163
163
|
proto.format = function (dateObj, arg, utc) {
|
|
164
164
|
var ctx = this || date, pattern = typeof arg === 'string' ? ctx.compile(arg) : arg,
|
|
@@ -175,10 +175,11 @@ proto.format = function (dateObj, arg, utc) {
|
|
|
175
175
|
};
|
|
176
176
|
|
|
177
177
|
/**
|
|
178
|
-
* Pre-parsing
|
|
179
|
-
* @param {string} dateString -
|
|
180
|
-
* @param {string|Array.<string>} arg -
|
|
181
|
-
* @
|
|
178
|
+
* Pre-parsing date and time strings
|
|
179
|
+
* @param {string} dateString - A date and time string
|
|
180
|
+
* @param {string|Array.<string>} arg - A format string or its compiled object
|
|
181
|
+
* @param {boolean} [utc] - Input as UTC
|
|
182
|
+
* @returns {Object} A pre-parsed result object
|
|
182
183
|
*/
|
|
183
184
|
proto.preparse = function (dateString, arg) {
|
|
184
185
|
var ctx = this || date, pattern = typeof arg === 'string' ? ctx.compile(arg) : arg,
|
|
@@ -214,18 +215,19 @@ proto.preparse = function (dateString, arg) {
|
|
|
214
215
|
};
|
|
215
216
|
|
|
216
217
|
/**
|
|
217
|
-
* Parsing
|
|
218
|
-
* @param {string} dateString -
|
|
219
|
-
* @param {string|Array.<string>} arg -
|
|
220
|
-
* @param {boolean} [utc] -
|
|
221
|
-
* @returns {Date}
|
|
218
|
+
* Parsing of date and time string (String -> Date)
|
|
219
|
+
* @param {string} dateString - A date-time string
|
|
220
|
+
* @param {string|Array.<string>} arg - A format string or its compiled object
|
|
221
|
+
* @param {boolean} [utc] - Input as UTC
|
|
222
|
+
* @returns {Date} A Date object
|
|
222
223
|
*/
|
|
223
224
|
proto.parse = function (dateString, arg, utc) {
|
|
224
|
-
var ctx = this || date,
|
|
225
|
+
var ctx = this || date, pattern = typeof arg === 'string' ? ctx.compile(arg) : arg,
|
|
226
|
+
dt = ctx.preparse(dateString, pattern);
|
|
225
227
|
|
|
226
228
|
if (ctx.isValid(dt)) {
|
|
227
229
|
dt.M -= dt.Y < 100 ? 22801 : 1; // 22801 = 1900 * 12 + 1
|
|
228
|
-
if (utc ||
|
|
230
|
+
if (utc || ~ctx._parser.find(pattern, 'ZZ').value) {
|
|
229
231
|
return new Date(Date.UTC(dt.Y, dt.M, dt.D, dt.H, dt.m + dt.Z, dt.s, dt.S));
|
|
230
232
|
}
|
|
231
233
|
return new Date(dt.Y, dt.M, dt.D, dt.H, dt.m, dt.s, dt.S);
|
|
@@ -234,10 +236,10 @@ proto.parse = function (dateString, arg, utc) {
|
|
|
234
236
|
};
|
|
235
237
|
|
|
236
238
|
/**
|
|
237
|
-
*
|
|
238
|
-
* @param {Object|string} arg1 -
|
|
239
|
-
* @param {string|Array.<string>} [arg2] -
|
|
240
|
-
* @returns {boolean}
|
|
239
|
+
* Date and time string validation
|
|
240
|
+
* @param {Object|string} arg1 - A pre-parsed result object or a date and time string
|
|
241
|
+
* @param {string|Array.<string>} [arg2] - A format string or its compiled object
|
|
242
|
+
* @returns {boolean} Whether the date and time string is a valid date and time
|
|
241
243
|
*/
|
|
242
244
|
proto.isValid = function (arg1, arg2) {
|
|
243
245
|
var ctx = this || date, dt = typeof arg1 === 'string' ? ctx.preparse(arg1, arg2) : arg1,
|
|
@@ -252,12 +254,12 @@ proto.isValid = function (arg1, arg2) {
|
|
|
252
254
|
};
|
|
253
255
|
|
|
254
256
|
/**
|
|
255
|
-
*
|
|
256
|
-
* @param {string} dateString -
|
|
257
|
-
* @param {string|Array.<string>} arg1 -
|
|
258
|
-
* @param {string|Array.<string>} arg2 -
|
|
259
|
-
* @param {boolean} [utc] -
|
|
260
|
-
* @returns {string}
|
|
257
|
+
* Format transformation of date and time string (String -> String)
|
|
258
|
+
* @param {string} dateString - A date and time string
|
|
259
|
+
* @param {string|Array.<string>} arg1 - A format string or its compiled object before transformation
|
|
260
|
+
* @param {string|Array.<string>} arg2 - A format string or its compiled object after transformation
|
|
261
|
+
* @param {boolean} [utc] - Output as UTC
|
|
262
|
+
* @returns {string} A formatted string
|
|
261
263
|
*/
|
|
262
264
|
proto.transform = function (dateString, arg1, arg2, utc) {
|
|
263
265
|
const ctx = this || date;
|
|
@@ -266,9 +268,9 @@ proto.transform = function (dateString, arg1, arg2, utc) {
|
|
|
266
268
|
|
|
267
269
|
/**
|
|
268
270
|
* Adding years
|
|
269
|
-
* @param {Date} dateObj -
|
|
270
|
-
* @param {number} years -
|
|
271
|
-
* @returns {Date}
|
|
271
|
+
* @param {Date} dateObj - A Date object
|
|
272
|
+
* @param {number} years - Number of years to add
|
|
273
|
+
* @returns {Date} The Date object after adding the value
|
|
272
274
|
*/
|
|
273
275
|
proto.addYears = function (dateObj, years) {
|
|
274
276
|
return (this || date).addMonths(dateObj, years * 12);
|
|
@@ -276,9 +278,9 @@ proto.addYears = function (dateObj, years) {
|
|
|
276
278
|
|
|
277
279
|
/**
|
|
278
280
|
* Adding months
|
|
279
|
-
* @param {Date} dateObj -
|
|
280
|
-
* @param {number} months -
|
|
281
|
-
* @returns {Date}
|
|
281
|
+
* @param {Date} dateObj - A Date object
|
|
282
|
+
* @param {number} months - Number of months to add
|
|
283
|
+
* @returns {Date} The Date object after adding the value
|
|
282
284
|
*/
|
|
283
285
|
proto.addMonths = function (dateObj, months) {
|
|
284
286
|
var d = new Date(dateObj.getTime());
|
|
@@ -289,9 +291,9 @@ proto.addMonths = function (dateObj, months) {
|
|
|
289
291
|
|
|
290
292
|
/**
|
|
291
293
|
* Adding days
|
|
292
|
-
* @param {Date} dateObj -
|
|
293
|
-
* @param {number} days -
|
|
294
|
-
* @returns {Date}
|
|
294
|
+
* @param {Date} dateObj - A Date object
|
|
295
|
+
* @param {number} days - Number of days to add
|
|
296
|
+
* @returns {Date} The Date object after adding the value
|
|
295
297
|
*/
|
|
296
298
|
proto.addDays = function (dateObj, days) {
|
|
297
299
|
var d = new Date(dateObj.getTime());
|
|
@@ -302,9 +304,9 @@ proto.addDays = function (dateObj, days) {
|
|
|
302
304
|
|
|
303
305
|
/**
|
|
304
306
|
* Adding hours
|
|
305
|
-
* @param {Date} dateObj -
|
|
306
|
-
* @param {number} hours -
|
|
307
|
-
* @returns {Date}
|
|
307
|
+
* @param {Date} dateObj - A Date object
|
|
308
|
+
* @param {number} hours - Number of hours to add
|
|
309
|
+
* @returns {Date} The Date object after adding the value
|
|
308
310
|
*/
|
|
309
311
|
proto.addHours = function (dateObj, hours) {
|
|
310
312
|
return (this || date).addMinutes(dateObj, hours * 60);
|
|
@@ -312,9 +314,9 @@ proto.addHours = function (dateObj, hours) {
|
|
|
312
314
|
|
|
313
315
|
/**
|
|
314
316
|
* Adding minutes
|
|
315
|
-
* @param {Date} dateObj -
|
|
316
|
-
* @param {number} minutes -
|
|
317
|
-
* @returns {Date}
|
|
317
|
+
* @param {Date} dateObj - A Date object
|
|
318
|
+
* @param {number} minutes - Number of minutes to add
|
|
319
|
+
* @returns {Date} The Date object after adding the value
|
|
318
320
|
*/
|
|
319
321
|
proto.addMinutes = function (dateObj, minutes) {
|
|
320
322
|
return (this || date).addSeconds(dateObj, minutes * 60);
|
|
@@ -322,9 +324,9 @@ proto.addMinutes = function (dateObj, minutes) {
|
|
|
322
324
|
|
|
323
325
|
/**
|
|
324
326
|
* Adding seconds
|
|
325
|
-
* @param {Date} dateObj -
|
|
326
|
-
* @param {number} seconds -
|
|
327
|
-
* @returns {Date}
|
|
327
|
+
* @param {Date} dateObj - A Date object
|
|
328
|
+
* @param {number} seconds - Number of seconds to add
|
|
329
|
+
* @returns {Date} The Date object after adding the value
|
|
328
330
|
*/
|
|
329
331
|
proto.addSeconds = function (dateObj, seconds) {
|
|
330
332
|
return (this || date).addMilliseconds(dateObj, seconds * 1000);
|
|
@@ -332,19 +334,19 @@ proto.addSeconds = function (dateObj, seconds) {
|
|
|
332
334
|
|
|
333
335
|
/**
|
|
334
336
|
* Adding milliseconds
|
|
335
|
-
* @param {Date} dateObj -
|
|
336
|
-
* @param {number} milliseconds -
|
|
337
|
-
* @returns {Date}
|
|
337
|
+
* @param {Date} dateObj - A Date object
|
|
338
|
+
* @param {number} milliseconds - Number of milliseconds to add
|
|
339
|
+
* @returns {Date} The Date object after adding the value
|
|
338
340
|
*/
|
|
339
341
|
proto.addMilliseconds = function (dateObj, milliseconds) {
|
|
340
342
|
return new Date(dateObj.getTime() + milliseconds);
|
|
341
343
|
};
|
|
342
344
|
|
|
343
345
|
/**
|
|
344
|
-
* Subtracting two dates
|
|
345
|
-
* @param {Date} date1 -
|
|
346
|
-
* @param {Date} date2 -
|
|
347
|
-
* @returns {Object}
|
|
346
|
+
* Subtracting two dates (date1 - date2)
|
|
347
|
+
* @param {Date} date1 - A Date object
|
|
348
|
+
* @param {Date} date2 - A Date object
|
|
349
|
+
* @returns {Object} The result object of subtracting date2 from date1
|
|
348
350
|
*/
|
|
349
351
|
proto.subtract = function (date1, date2) {
|
|
350
352
|
var delta = date1.getTime() - date2.getTime();
|
|
@@ -369,9 +371,9 @@ proto.subtract = function (date1, date2) {
|
|
|
369
371
|
};
|
|
370
372
|
|
|
371
373
|
/**
|
|
372
|
-
* Whether year is leap year
|
|
373
|
-
* @param {number} y - year
|
|
374
|
-
* @returns {boolean}
|
|
374
|
+
* Whether a year is a leap year
|
|
375
|
+
* @param {number} y - A year to check
|
|
376
|
+
* @returns {boolean} Whether the year is a leap year
|
|
375
377
|
*/
|
|
376
378
|
proto.isLeapYear = function (y) {
|
|
377
379
|
return (!(y % 4) && !!(y % 100)) || !(y % 400);
|
|
@@ -379,19 +381,19 @@ proto.isLeapYear = function (y) {
|
|
|
379
381
|
|
|
380
382
|
/**
|
|
381
383
|
* Comparison of two dates
|
|
382
|
-
* @param {Date} date1 -
|
|
383
|
-
* @param {Date} date2 -
|
|
384
|
-
* @returns {boolean}
|
|
384
|
+
* @param {Date} date1 - A Date object
|
|
385
|
+
* @param {Date} date2 - A Date object
|
|
386
|
+
* @returns {boolean} Whether the two dates are the same day (time is ignored)
|
|
385
387
|
*/
|
|
386
388
|
proto.isSameDay = function (date1, date2) {
|
|
387
389
|
return date1.toDateString() === date2.toDateString();
|
|
388
390
|
};
|
|
389
391
|
|
|
390
392
|
/**
|
|
391
|
-
*
|
|
392
|
-
* @param {string} code - language code
|
|
393
|
-
* @param {Function} locale - locale installer
|
|
394
|
-
* @returns {
|
|
393
|
+
* Definition of new locale
|
|
394
|
+
* @param {string} code - A language code
|
|
395
|
+
* @param {Function} locale - A locale installer
|
|
396
|
+
* @returns {void}
|
|
395
397
|
*/
|
|
396
398
|
proto.locale = function (code, locale) {
|
|
397
399
|
if (!locales[code]) {
|
|
@@ -400,9 +402,9 @@ proto.locale = function (code, locale) {
|
|
|
400
402
|
};
|
|
401
403
|
|
|
402
404
|
/**
|
|
403
|
-
*
|
|
404
|
-
* @param {string} name - plugin name
|
|
405
|
-
* @param {Function} plugin - plugin installer
|
|
405
|
+
* Definition of new plugin
|
|
406
|
+
* @param {string} name - A plugin name
|
|
407
|
+
* @param {Function} plugin - A plugin installer
|
|
406
408
|
* @returns {void}
|
|
407
409
|
*/
|
|
408
410
|
proto.plugin = function (name, plugin) {
|
|
@@ -415,9 +417,9 @@ localized_proto = extend(proto);
|
|
|
415
417
|
date = extend(proto);
|
|
416
418
|
|
|
417
419
|
/**
|
|
418
|
-
* Changing
|
|
419
|
-
* @param {Function|string} [locale] - locale
|
|
420
|
-
* @returns {string} current language code
|
|
420
|
+
* Changing locales
|
|
421
|
+
* @param {Function|string} [locale] - A locale installer or language code
|
|
422
|
+
* @returns {string} The current language code
|
|
421
423
|
*/
|
|
422
424
|
date.locale = function (locale) {
|
|
423
425
|
var install = typeof locale === 'function' ? locale : date.locale[locale];
|
|
@@ -443,8 +445,8 @@ date.locale = function (locale) {
|
|
|
443
445
|
};
|
|
444
446
|
|
|
445
447
|
/**
|
|
446
|
-
*
|
|
447
|
-
* @param {Object} extension - extension object
|
|
448
|
+
* Functional extension
|
|
449
|
+
* @param {Object} extension - An extension object
|
|
448
450
|
* @returns {void}
|
|
449
451
|
*/
|
|
450
452
|
date.extend = function (extension) {
|
|
@@ -462,8 +464,8 @@ date.extend = function (extension) {
|
|
|
462
464
|
};
|
|
463
465
|
|
|
464
466
|
/**
|
|
465
|
-
* Importing
|
|
466
|
-
* @param {Function|string} plugin - plugin
|
|
467
|
+
* Importing plugins
|
|
468
|
+
* @param {Function|string} plugin - A plugin installer or plugin name
|
|
467
469
|
* @returns {void}
|
|
468
470
|
*/
|
|
469
471
|
date.plugin = function (plugin) {
|
|
@@ -7,12 +7,12 @@ h:function(a){return""+(a.getHours()%12||12)},mm:function(a){return("0"+a.getMin
|
|
|
7
7
|
dd:function(a){return this.res.dd[a.getDay()]},Z:function(a){a=a.getTimezoneOffset()/.6|0;return(0<a?"-":"+")+("000"+Math.abs(a-(a%100*.4|0))).slice(-4)},ZZ:function(a){a=a.getTimezoneOffset();var b=Math.abs(a);return(0<a?"-":"+")+("0"+(b/60|0)).slice(-2)+":"+("0"+b%60).slice(-2)},post:function(a){return a},res:n},r={YYYY:function(a){return this.exec(/^\d{4}/,a)},Y:function(a){return this.exec(/^\d{1,4}/,a)},MMMM:function(a){a=this.find(this.res.MMMM,a);a.value++;return a},MMM:function(a){a=this.find(this.res.MMM,
|
|
8
8
|
a);a.value++;return a},MM:function(a){return this.exec(/^\d\d/,a)},M:function(a){return this.exec(/^\d\d?/,a)},DD:function(a){return this.exec(/^\d\d/,a)},D:function(a){return this.exec(/^\d\d?/,a)},HH:function(a){return this.exec(/^\d\d/,a)},H:function(a){return this.exec(/^\d\d?/,a)},A:function(a){return this.find(this.res.A,a)},hh:function(a){return this.exec(/^\d\d/,a)},h:function(a){return this.exec(/^\d\d?/,a)},mm:function(a){return this.exec(/^\d\d/,a)},m:function(a){return this.exec(/^\d\d?/,
|
|
9
9
|
a)},ss:function(a){return this.exec(/^\d\d/,a)},s:function(a){return this.exec(/^\d\d?/,a)},SSS:function(a){return this.exec(/^\d{1,3}/,a)},SS:function(a){a=this.exec(/^\d\d?/,a);a.value*=10;return a},S:function(a){a=this.exec(/^\d/,a);a.value*=100;return a},Z:function(a){a=this.exec(/^[\+-]\d{2}[0-5]\d/,a);a.value=-60*(a.value/100|0)-a.value%100;return a},ZZ:function(a){a=/^([\+-])(\d{2}):([0-5]\d)/.exec(a)||["","","",""];return{value:-(60*(a[1]+a[2]|0)+(a[1]+a[3]|0)),length:a[0].length}},h12:function(a,
|
|
10
|
-
b){return(12===a?0:a)+12*b},exec:function(a,b){a=(a.exec(b)||[""])[0];return{value:a|0,length:a.length}},find:function(a,b){for(var
|
|
11
|
-
u.compile=function(a){for(var b=/\[([^\[\]]|\[[^\[\]]*])*]|([A-Za-z])\2+|\.{3}|./g,c
|
|
12
|
-
u.preparse=function(a,b){var
|
|
13
|
-
|
|
14
|
-
u.isValid=function(a,b){var
|
|
10
|
+
b){return(12===a?0:a)+12*b},exec:function(a,b){a=(a.exec(b)||[""])[0];return{value:a|0,length:a.length}},find:function(a,b){for(var d=-1,c=0,h=0,g=a.length,l;h<g;h++)l=a[h],!b.indexOf(l)&&l.length>c&&(d=h,c=l.length);return{value:d,length:c}},pre:function(a){return a},res:n};function t(a,b,d,c){var h={},g;for(g in a)h[g]=a[g];for(g in b||{})!!d^!!h[g]||(h[g]=b[g]);c&&(h.res=c);return h}var u={_formatter:q,_parser:r},w,x;
|
|
11
|
+
u.compile=function(a){for(var b=/\[([^\[\]]|\[[^\[\]]*])*]|([A-Za-z])\2+|\.{3}|./g,d,c=[a];d=b.exec(a);)c[c.length]=d[0];return c};u.format=function(a,b,d){var c=this||x;b="string"===typeof b?c.compile(b):b;var h=a.getTimezoneOffset();a=c.addMinutes(a,d?h:0);c=c._formatter;var g="";a.getTimezoneOffset=function(){return d?0:h};for(var l=1,v=b.length,k;l<v;l++)k=b[l],g+=c[k]?c.post(c[k](a,b[0])):k.replace(/\[(.*)]/,"$1");return g};
|
|
12
|
+
u.preparse=function(a,b){var d=this||x;b="string"===typeof b?d.compile(b):b;var c={Y:1970,M:1,D:1,H:0,A:0,h:0,m:0,s:0,S:0,Z:0,_index:0,_length:0,_match:0},h=/\[(.*)]/;d=d._parser;var g=0;a=d.pre(a);for(var l=1,v=b.length,k,p;l<v;l++)if(k=b[l],d[k]){p=d[k](a.slice(g),b[0]);if(!p.length)break;g+=p.length;c[p.token||k.charAt(0)]=p.value;c._match++}else if(k===a.charAt(g)||" "===k)g++;else if(h.test(k)&&!a.slice(g).indexOf(h.exec(k)[1]))g+=k.length-2;else{"..."===k&&(g=a.length);break}c.H=c.H||d.h12(c.h,
|
|
13
|
+
c.A);c._index=g;c._length=a.length;return c};u.parse=function(a,b,d){var c=this||x;b="string"===typeof b?c.compile(b):b;a=c.preparse(a,b);return c.isValid(a)?(a.M-=100>a.Y?22801:1,d||~c._parser.find(b,"ZZ").value?new Date(Date.UTC(a.Y,a.M,a.D,a.H,a.m+a.Z,a.s,a.S)):new Date(a.Y,a.M,a.D,a.H,a.m,a.s,a.S)):new Date(NaN)};
|
|
14
|
+
u.isValid=function(a,b){var d=this||x;a="string"===typeof a?d.preparse(a,b):a;d=[31,28+d.isLeapYear(a.Y)|0,31,30,31,30,31,31,30,31,30,31][a.M-1];return!(1>a._index||1>a._length||a._index-a._length||1>a._match||1>a.Y||9999<a.Y||1>a.M||12<a.M||1>a.D||a.D>d||0>a.H||23<a.H||0>a.m||59<a.m||0>a.s||59<a.s||0>a.S||999<a.S||-840>a.Z||720<a.Z)};u.transform=function(a,b,d,c){let h=this||x;return h.format(h.parse(a,b),d,c)};u.addYears=function(a,b){return(this||x).addMonths(a,12*b)};
|
|
15
15
|
u.addMonths=function(a,b){a=new Date(a.getTime());a.setMonth(a.getMonth()+b);return a};u.addDays=function(a,b){a=new Date(a.getTime());a.setDate(a.getDate()+b);return a};u.addHours=function(a,b){return(this||x).addMinutes(a,60*b)};u.addMinutes=function(a,b){return(this||x).addSeconds(a,60*b)};u.addSeconds=function(a,b){return(this||x).addMilliseconds(a,1E3*b)};u.addMilliseconds=function(a,b){return new Date(a.getTime()+b)};
|
|
16
|
-
u.subtract=function(a,b){var
|
|
17
|
-
x.locale=function(a){a="function"===typeof a?a:x.locale[a];if(!a)return m;m=a(u);var b=e[m]||{},
|
|
16
|
+
u.subtract=function(a,b){var d=a.getTime()-b.getTime();return{toMilliseconds:function(){return d},toSeconds:function(){return d/1E3},toMinutes:function(){return d/6E4},toHours:function(){return d/36E5},toDays:function(){return d/864E5}}};u.isLeapYear=function(a){return!(a%4)&&!!(a%100)||!(a%400)};u.isSameDay=function(a,b){return a.toDateString()===b.toDateString()};u.locale=function(a,b){e[a]||(e[a]=b)};u.plugin=function(a,b){f[a]||(f[a]=b)};w=t(u);x=t(u);
|
|
17
|
+
x.locale=function(a){a="function"===typeof a?a:x.locale[a];if(!a)return m;m=a(u);var b=e[m]||{},d=t(n,b.res,!0);a=t(q,b.formatter,!0,d);b=t(r,b.parser,!0,d);x._formatter=w._formatter=a;x._parser=w._parser=b;for(var c in f)x.extend(f[c]);return m};x.extend=function(a){var b=t(x._parser.res,a.res),d=a.extender||{};x._formatter=t(x._formatter,a.formatter,!1,b);x._parser=t(x._parser,a.parser,!1,b);for(var c in d)x[c]||(x[c]=d[c])};
|
|
18
18
|
x.plugin=function(a){(a="function"===typeof a?a:x.plugin[a])&&x.extend(f[a(u,w)]||{})};export default x
|
package/esm/date-and-time.mjs
CHANGED
|
@@ -140,9 +140,9 @@ var locales = {},
|
|
|
140
140
|
date;
|
|
141
141
|
|
|
142
142
|
/**
|
|
143
|
-
* Compiling
|
|
144
|
-
* @param {string} formatString -
|
|
145
|
-
* @returns {Array.<string>}
|
|
143
|
+
* Compiling format strings
|
|
144
|
+
* @param {string} formatString - A format string
|
|
145
|
+
* @returns {Array.<string>} A compiled object
|
|
146
146
|
*/
|
|
147
147
|
proto.compile = function (formatString) {
|
|
148
148
|
var re = /\[([^\[\]]|\[[^\[\]]*])*]|([A-Za-z])\2+|\.{3}|./g, keys, pattern = [formatString];
|
|
@@ -154,11 +154,11 @@ proto.compile = function (formatString) {
|
|
|
154
154
|
};
|
|
155
155
|
|
|
156
156
|
/**
|
|
157
|
-
* Formatting
|
|
158
|
-
* @param {Date} dateObj -
|
|
159
|
-
* @param {string|Array.<string>} arg -
|
|
160
|
-
* @param {boolean} [utc] -
|
|
161
|
-
* @returns {string}
|
|
157
|
+
* Formatting date and time objects (Date -> String)
|
|
158
|
+
* @param {Date} dateObj - A Date object
|
|
159
|
+
* @param {string|Array.<string>} arg - A format string or its compiled object
|
|
160
|
+
* @param {boolean} [utc] - Output as UTC
|
|
161
|
+
* @returns {string} A formatted string
|
|
162
162
|
*/
|
|
163
163
|
proto.format = function (dateObj, arg, utc) {
|
|
164
164
|
var ctx = this || date, pattern = typeof arg === 'string' ? ctx.compile(arg) : arg,
|
|
@@ -175,10 +175,11 @@ proto.format = function (dateObj, arg, utc) {
|
|
|
175
175
|
};
|
|
176
176
|
|
|
177
177
|
/**
|
|
178
|
-
* Pre-parsing
|
|
179
|
-
* @param {string} dateString -
|
|
180
|
-
* @param {string|Array.<string>} arg -
|
|
181
|
-
* @
|
|
178
|
+
* Pre-parsing date and time strings
|
|
179
|
+
* @param {string} dateString - A date and time string
|
|
180
|
+
* @param {string|Array.<string>} arg - A format string or its compiled object
|
|
181
|
+
* @param {boolean} [utc] - Input as UTC
|
|
182
|
+
* @returns {Object} A pre-parsed result object
|
|
182
183
|
*/
|
|
183
184
|
proto.preparse = function (dateString, arg) {
|
|
184
185
|
var ctx = this || date, pattern = typeof arg === 'string' ? ctx.compile(arg) : arg,
|
|
@@ -214,18 +215,19 @@ proto.preparse = function (dateString, arg) {
|
|
|
214
215
|
};
|
|
215
216
|
|
|
216
217
|
/**
|
|
217
|
-
* Parsing
|
|
218
|
-
* @param {string} dateString -
|
|
219
|
-
* @param {string|Array.<string>} arg -
|
|
220
|
-
* @param {boolean} [utc] -
|
|
221
|
-
* @returns {Date}
|
|
218
|
+
* Parsing of date and time string (String -> Date)
|
|
219
|
+
* @param {string} dateString - A date-time string
|
|
220
|
+
* @param {string|Array.<string>} arg - A format string or its compiled object
|
|
221
|
+
* @param {boolean} [utc] - Input as UTC
|
|
222
|
+
* @returns {Date} A Date object
|
|
222
223
|
*/
|
|
223
224
|
proto.parse = function (dateString, arg, utc) {
|
|
224
|
-
var ctx = this || date,
|
|
225
|
+
var ctx = this || date, pattern = typeof arg === 'string' ? ctx.compile(arg) : arg,
|
|
226
|
+
dt = ctx.preparse(dateString, pattern);
|
|
225
227
|
|
|
226
228
|
if (ctx.isValid(dt)) {
|
|
227
229
|
dt.M -= dt.Y < 100 ? 22801 : 1; // 22801 = 1900 * 12 + 1
|
|
228
|
-
if (utc ||
|
|
230
|
+
if (utc || ~ctx._parser.find(pattern, 'ZZ').value) {
|
|
229
231
|
return new Date(Date.UTC(dt.Y, dt.M, dt.D, dt.H, dt.m + dt.Z, dt.s, dt.S));
|
|
230
232
|
}
|
|
231
233
|
return new Date(dt.Y, dt.M, dt.D, dt.H, dt.m, dt.s, dt.S);
|
|
@@ -234,10 +236,10 @@ proto.parse = function (dateString, arg, utc) {
|
|
|
234
236
|
};
|
|
235
237
|
|
|
236
238
|
/**
|
|
237
|
-
*
|
|
238
|
-
* @param {Object|string} arg1 -
|
|
239
|
-
* @param {string|Array.<string>} [arg2] -
|
|
240
|
-
* @returns {boolean}
|
|
239
|
+
* Date and time string validation
|
|
240
|
+
* @param {Object|string} arg1 - A pre-parsed result object or a date and time string
|
|
241
|
+
* @param {string|Array.<string>} [arg2] - A format string or its compiled object
|
|
242
|
+
* @returns {boolean} Whether the date and time string is a valid date and time
|
|
241
243
|
*/
|
|
242
244
|
proto.isValid = function (arg1, arg2) {
|
|
243
245
|
var ctx = this || date, dt = typeof arg1 === 'string' ? ctx.preparse(arg1, arg2) : arg1,
|
|
@@ -252,12 +254,12 @@ proto.isValid = function (arg1, arg2) {
|
|
|
252
254
|
};
|
|
253
255
|
|
|
254
256
|
/**
|
|
255
|
-
*
|
|
256
|
-
* @param {string} dateString -
|
|
257
|
-
* @param {string|Array.<string>} arg1 -
|
|
258
|
-
* @param {string|Array.<string>} arg2 -
|
|
259
|
-
* @param {boolean} [utc] -
|
|
260
|
-
* @returns {string}
|
|
257
|
+
* Format transformation of date and time string (String -> String)
|
|
258
|
+
* @param {string} dateString - A date and time string
|
|
259
|
+
* @param {string|Array.<string>} arg1 - A format string or its compiled object before transformation
|
|
260
|
+
* @param {string|Array.<string>} arg2 - A format string or its compiled object after transformation
|
|
261
|
+
* @param {boolean} [utc] - Output as UTC
|
|
262
|
+
* @returns {string} A formatted string
|
|
261
263
|
*/
|
|
262
264
|
proto.transform = function (dateString, arg1, arg2, utc) {
|
|
263
265
|
const ctx = this || date;
|
|
@@ -266,9 +268,9 @@ proto.transform = function (dateString, arg1, arg2, utc) {
|
|
|
266
268
|
|
|
267
269
|
/**
|
|
268
270
|
* Adding years
|
|
269
|
-
* @param {Date} dateObj -
|
|
270
|
-
* @param {number} years -
|
|
271
|
-
* @returns {Date}
|
|
271
|
+
* @param {Date} dateObj - A Date object
|
|
272
|
+
* @param {number} years - Number of years to add
|
|
273
|
+
* @returns {Date} The Date object after adding the value
|
|
272
274
|
*/
|
|
273
275
|
proto.addYears = function (dateObj, years) {
|
|
274
276
|
return (this || date).addMonths(dateObj, years * 12);
|
|
@@ -276,9 +278,9 @@ proto.addYears = function (dateObj, years) {
|
|
|
276
278
|
|
|
277
279
|
/**
|
|
278
280
|
* Adding months
|
|
279
|
-
* @param {Date} dateObj -
|
|
280
|
-
* @param {number} months -
|
|
281
|
-
* @returns {Date}
|
|
281
|
+
* @param {Date} dateObj - A Date object
|
|
282
|
+
* @param {number} months - Number of months to add
|
|
283
|
+
* @returns {Date} The Date object after adding the value
|
|
282
284
|
*/
|
|
283
285
|
proto.addMonths = function (dateObj, months) {
|
|
284
286
|
var d = new Date(dateObj.getTime());
|
|
@@ -289,9 +291,9 @@ proto.addMonths = function (dateObj, months) {
|
|
|
289
291
|
|
|
290
292
|
/**
|
|
291
293
|
* Adding days
|
|
292
|
-
* @param {Date} dateObj -
|
|
293
|
-
* @param {number} days -
|
|
294
|
-
* @returns {Date}
|
|
294
|
+
* @param {Date} dateObj - A Date object
|
|
295
|
+
* @param {number} days - Number of days to add
|
|
296
|
+
* @returns {Date} The Date object after adding the value
|
|
295
297
|
*/
|
|
296
298
|
proto.addDays = function (dateObj, days) {
|
|
297
299
|
var d = new Date(dateObj.getTime());
|
|
@@ -302,9 +304,9 @@ proto.addDays = function (dateObj, days) {
|
|
|
302
304
|
|
|
303
305
|
/**
|
|
304
306
|
* Adding hours
|
|
305
|
-
* @param {Date} dateObj -
|
|
306
|
-
* @param {number} hours -
|
|
307
|
-
* @returns {Date}
|
|
307
|
+
* @param {Date} dateObj - A Date object
|
|
308
|
+
* @param {number} hours - Number of hours to add
|
|
309
|
+
* @returns {Date} The Date object after adding the value
|
|
308
310
|
*/
|
|
309
311
|
proto.addHours = function (dateObj, hours) {
|
|
310
312
|
return (this || date).addMinutes(dateObj, hours * 60);
|
|
@@ -312,9 +314,9 @@ proto.addHours = function (dateObj, hours) {
|
|
|
312
314
|
|
|
313
315
|
/**
|
|
314
316
|
* Adding minutes
|
|
315
|
-
* @param {Date} dateObj -
|
|
316
|
-
* @param {number} minutes -
|
|
317
|
-
* @returns {Date}
|
|
317
|
+
* @param {Date} dateObj - A Date object
|
|
318
|
+
* @param {number} minutes - Number of minutes to add
|
|
319
|
+
* @returns {Date} The Date object after adding the value
|
|
318
320
|
*/
|
|
319
321
|
proto.addMinutes = function (dateObj, minutes) {
|
|
320
322
|
return (this || date).addSeconds(dateObj, minutes * 60);
|
|
@@ -322,9 +324,9 @@ proto.addMinutes = function (dateObj, minutes) {
|
|
|
322
324
|
|
|
323
325
|
/**
|
|
324
326
|
* Adding seconds
|
|
325
|
-
* @param {Date} dateObj -
|
|
326
|
-
* @param {number} seconds -
|
|
327
|
-
* @returns {Date}
|
|
327
|
+
* @param {Date} dateObj - A Date object
|
|
328
|
+
* @param {number} seconds - Number of seconds to add
|
|
329
|
+
* @returns {Date} The Date object after adding the value
|
|
328
330
|
*/
|
|
329
331
|
proto.addSeconds = function (dateObj, seconds) {
|
|
330
332
|
return (this || date).addMilliseconds(dateObj, seconds * 1000);
|
|
@@ -332,19 +334,19 @@ proto.addSeconds = function (dateObj, seconds) {
|
|
|
332
334
|
|
|
333
335
|
/**
|
|
334
336
|
* Adding milliseconds
|
|
335
|
-
* @param {Date} dateObj -
|
|
336
|
-
* @param {number} milliseconds -
|
|
337
|
-
* @returns {Date}
|
|
337
|
+
* @param {Date} dateObj - A Date object
|
|
338
|
+
* @param {number} milliseconds - Number of milliseconds to add
|
|
339
|
+
* @returns {Date} The Date object after adding the value
|
|
338
340
|
*/
|
|
339
341
|
proto.addMilliseconds = function (dateObj, milliseconds) {
|
|
340
342
|
return new Date(dateObj.getTime() + milliseconds);
|
|
341
343
|
};
|
|
342
344
|
|
|
343
345
|
/**
|
|
344
|
-
* Subtracting two dates
|
|
345
|
-
* @param {Date} date1 -
|
|
346
|
-
* @param {Date} date2 -
|
|
347
|
-
* @returns {Object}
|
|
346
|
+
* Subtracting two dates (date1 - date2)
|
|
347
|
+
* @param {Date} date1 - A Date object
|
|
348
|
+
* @param {Date} date2 - A Date object
|
|
349
|
+
* @returns {Object} The result object of subtracting date2 from date1
|
|
348
350
|
*/
|
|
349
351
|
proto.subtract = function (date1, date2) {
|
|
350
352
|
var delta = date1.getTime() - date2.getTime();
|
|
@@ -369,9 +371,9 @@ proto.subtract = function (date1, date2) {
|
|
|
369
371
|
};
|
|
370
372
|
|
|
371
373
|
/**
|
|
372
|
-
* Whether year is leap year
|
|
373
|
-
* @param {number} y - year
|
|
374
|
-
* @returns {boolean}
|
|
374
|
+
* Whether a year is a leap year
|
|
375
|
+
* @param {number} y - A year to check
|
|
376
|
+
* @returns {boolean} Whether the year is a leap year
|
|
375
377
|
*/
|
|
376
378
|
proto.isLeapYear = function (y) {
|
|
377
379
|
return (!(y % 4) && !!(y % 100)) || !(y % 400);
|
|
@@ -379,19 +381,19 @@ proto.isLeapYear = function (y) {
|
|
|
379
381
|
|
|
380
382
|
/**
|
|
381
383
|
* Comparison of two dates
|
|
382
|
-
* @param {Date} date1 -
|
|
383
|
-
* @param {Date} date2 -
|
|
384
|
-
* @returns {boolean}
|
|
384
|
+
* @param {Date} date1 - A Date object
|
|
385
|
+
* @param {Date} date2 - A Date object
|
|
386
|
+
* @returns {boolean} Whether the two dates are the same day (time is ignored)
|
|
385
387
|
*/
|
|
386
388
|
proto.isSameDay = function (date1, date2) {
|
|
387
389
|
return date1.toDateString() === date2.toDateString();
|
|
388
390
|
};
|
|
389
391
|
|
|
390
392
|
/**
|
|
391
|
-
*
|
|
392
|
-
* @param {string} code - language code
|
|
393
|
-
* @param {Function} locale - locale installer
|
|
394
|
-
* @returns {
|
|
393
|
+
* Definition of new locale
|
|
394
|
+
* @param {string} code - A language code
|
|
395
|
+
* @param {Function} locale - A locale installer
|
|
396
|
+
* @returns {void}
|
|
395
397
|
*/
|
|
396
398
|
proto.locale = function (code, locale) {
|
|
397
399
|
if (!locales[code]) {
|
|
@@ -400,9 +402,9 @@ proto.locale = function (code, locale) {
|
|
|
400
402
|
};
|
|
401
403
|
|
|
402
404
|
/**
|
|
403
|
-
*
|
|
404
|
-
* @param {string} name - plugin name
|
|
405
|
-
* @param {Function} plugin - plugin installer
|
|
405
|
+
* Definition of new plugin
|
|
406
|
+
* @param {string} name - A plugin name
|
|
407
|
+
* @param {Function} plugin - A plugin installer
|
|
406
408
|
* @returns {void}
|
|
407
409
|
*/
|
|
408
410
|
proto.plugin = function (name, plugin) {
|
|
@@ -415,9 +417,9 @@ localized_proto = extend(proto);
|
|
|
415
417
|
date = extend(proto);
|
|
416
418
|
|
|
417
419
|
/**
|
|
418
|
-
* Changing
|
|
419
|
-
* @param {Function|string} [locale] - locale
|
|
420
|
-
* @returns {string} current language code
|
|
420
|
+
* Changing locales
|
|
421
|
+
* @param {Function|string} [locale] - A locale installer or language code
|
|
422
|
+
* @returns {string} The current language code
|
|
421
423
|
*/
|
|
422
424
|
date.locale = function (locale) {
|
|
423
425
|
var install = typeof locale === 'function' ? locale : date.locale[locale];
|
|
@@ -443,8 +445,8 @@ date.locale = function (locale) {
|
|
|
443
445
|
};
|
|
444
446
|
|
|
445
447
|
/**
|
|
446
|
-
*
|
|
447
|
-
* @param {Object} extension - extension object
|
|
448
|
+
* Functional extension
|
|
449
|
+
* @param {Object} extension - An extension object
|
|
448
450
|
* @returns {void}
|
|
449
451
|
*/
|
|
450
452
|
date.extend = function (extension) {
|
|
@@ -462,8 +464,8 @@ date.extend = function (extension) {
|
|
|
462
464
|
};
|
|
463
465
|
|
|
464
466
|
/**
|
|
465
|
-
* Importing
|
|
466
|
-
* @param {Function|string} plugin - plugin
|
|
467
|
+
* Importing plugins
|
|
468
|
+
* @param {Function|string} plugin - A plugin installer or plugin name
|
|
467
469
|
* @returns {void}
|
|
468
470
|
*/
|
|
469
471
|
date.plugin = function (plugin) {
|
|
@@ -30,10 +30,8 @@ var plugin = function (date, localized_date) {
|
|
|
30
30
|
var pattern2 = typeof arg === 'string' ? date.compile(arg) : arg;
|
|
31
31
|
var dateObj = localized_date.parse(dateString, pattern2, true);
|
|
32
32
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
return dateObj;
|
|
36
|
-
}
|
|
33
|
+
if (~date._parser.find(pattern2, 'ZZ').value) {
|
|
34
|
+
return dateObj;
|
|
37
35
|
}
|
|
38
36
|
|
|
39
37
|
options.timeZone = timeZone;
|
|
@@ -56,7 +54,7 @@ var plugin = function (date, localized_date) {
|
|
|
56
54
|
return d;
|
|
57
55
|
}
|
|
58
56
|
}
|
|
59
|
-
return NaN;
|
|
57
|
+
return new Date(NaN);
|
|
60
58
|
};
|
|
61
59
|
var transformTZ = function (dateString, arg1, arg2, timeZone) {
|
|
62
60
|
return formatTZ(localized_date.parse(dateString, arg1), arg2, timeZone);
|