date-and-time 3.2.0 → 3.4.0
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 +39 -0
- package/README.md +15 -14
- package/date-and-time.d.ts +1 -1
- package/date-and-time.js +13 -13
- package/date-and-time.min.js +1 -1
- package/esm/date-and-time.es.js +13 -13
- package/esm/date-and-time.es.min.js +1 -1
- package/esm/date-and-time.mjs +13 -13
- package/esm/plugin/timezone.es.js +177 -26
- package/esm/plugin/timezone.mjs +177 -26
- package/package.json +4 -4
- package/plugin/day-of-week.d.ts +1 -1
- package/plugin/meridiem.d.ts +1 -1
- package/plugin/microsecond.d.ts +1 -1
- package/plugin/ordinal.d.ts +1 -1
- package/plugin/timespan.d.ts +1 -1
- package/plugin/timezone.d.ts +1 -1
- package/plugin/timezone.js +177 -26
- package/plugin/two-digit-year.d.ts +1 -1
package/PLUGINS.md
CHANGED
|
@@ -380,6 +380,45 @@ In the first example above, if you want `parseTZ()` to parse the time as PST, yo
|
|
|
380
380
|
date.parse('Nov 7 2021 1:59:59 -0800', 'MMM D YYYY H:mm:ss Z'); // => 2021-11-07T09:59:59Z
|
|
381
381
|
```
|
|
382
382
|
|
|
383
|
+
#### Token Extension
|
|
384
|
+
|
|
385
|
+
This plugin also adds tokens for time zone name to the formatter.
|
|
386
|
+
|
|
387
|
+
**formatter:**
|
|
388
|
+
|
|
389
|
+
| token | meaning | output examples |
|
|
390
|
+
|:------|:----------------------------|:----------------------|
|
|
391
|
+
| z | time zone name abbreviation | PST, EST |
|
|
392
|
+
| zz | time zone name | Pacific Standard Time |
|
|
393
|
+
|
|
394
|
+
The `z` and `zz` are lowercase. Also, currently it does not support output other than English.
|
|
395
|
+
|
|
396
|
+
**parser:**
|
|
397
|
+
|
|
398
|
+
There is no change.
|
|
399
|
+
|
|
400
|
+
```javascript
|
|
401
|
+
const date = require('date-and-time');
|
|
402
|
+
// Import "timezone" plugin.
|
|
403
|
+
const timezone = require('date-and-time/plugin/timezone');
|
|
404
|
+
|
|
405
|
+
// Apply "timezone" plugin to the library.
|
|
406
|
+
date.plugin(timezone);
|
|
407
|
+
|
|
408
|
+
const d1 = new Date(Date.UTC(2021, 2, 14, 9, 59, 59, 999));
|
|
409
|
+
date.format(d1, 'MMMM DD YYYY H:mm:ss.SSS zz');
|
|
410
|
+
// March 14 2021 1:59:59.999 Pacific Standard Time
|
|
411
|
+
|
|
412
|
+
date.format(d1, 'MMMM DD YYYY H:mm:ss.SSS zz', true);
|
|
413
|
+
// March 14 2021 9:59:59.999 Coordinated Universal Time
|
|
414
|
+
|
|
415
|
+
date.formatTZ(d1, 'MMMM DD YYYY H:mm:ss.SSS z', 'Asia/Tokyo');
|
|
416
|
+
// March 14 2021 18:59:59.999 JST
|
|
417
|
+
|
|
418
|
+
// Transforms the date string from EST (Eastern Standard Time) to PDT (Pacific Daylight Time).
|
|
419
|
+
date.transform('2021-11-07T03:59:59 UTC-0500', 'YYYY-MM-DD[T]HH:mm:ss [UTC]Z', 'MMMM D YYYY H:mm:ss z');
|
|
420
|
+
// November 7 2021 1:59:59 PDT
|
|
421
|
+
```
|
|
383
422
|
---
|
|
384
423
|
|
|
385
424
|
### two-digit-year
|
package/README.md
CHANGED
|
@@ -25,16 +25,15 @@ npm i date-and-time
|
|
|
25
25
|
|
|
26
26
|
## Recent Changes
|
|
27
27
|
|
|
28
|
-
- 3.
|
|
29
|
-
-
|
|
28
|
+
- 3.4.0
|
|
29
|
+
- Added `zz` (time zone name) and `z` (time zone name abbreviation) tokens to the `timezone` plugin.
|
|
30
|
+
- Fixed an issue where token extensions by other plugins were not reflected in functions provided by the `timezone` plugin.
|
|
30
31
|
|
|
31
|
-
- 3.
|
|
32
|
-
-
|
|
33
|
-
- Refactored `formatTZ()` of `timezone` plugin.
|
|
32
|
+
- 3.3.0
|
|
33
|
+
- Refactored `format()`, `isValid()`, and `preparse()`, further improved performance.
|
|
34
34
|
|
|
35
|
-
- 3.
|
|
36
|
-
-
|
|
37
|
-
- Organized some test modules.
|
|
35
|
+
- 3.2.0
|
|
36
|
+
- Refactored `compile()`, `format()`, and `preparse()`, slightly improved performance.
|
|
38
37
|
|
|
39
38
|
## Usage
|
|
40
39
|
|
|
@@ -180,12 +179,14 @@ Available tokens and their meanings are as follows:
|
|
|
180
179
|
|
|
181
180
|
You can also use the following tokens by importing plugins. See [PLUGINS.md](./PLUGINS.md) for details.
|
|
182
181
|
|
|
183
|
-
| token | meaning | examples of output
|
|
184
|
-
|
|
185
|
-
| DDD | ordinal notation of date | 1st, 2nd, 3rd
|
|
186
|
-
| AA | meridiem (uppercase with ellipsis) | A.M., P.M.
|
|
187
|
-
| a | meridiem (lowercase) | am, pm
|
|
188
|
-
| aa | meridiem (lowercase with ellipsis) | a.m., p.m.
|
|
182
|
+
| token | meaning | examples of output |
|
|
183
|
+
|:------|:-------------------------------------|:----------------------|
|
|
184
|
+
| DDD | ordinal notation of date | 1st, 2nd, 3rd |
|
|
185
|
+
| AA | meridiem (uppercase with ellipsis) | A.M., P.M. |
|
|
186
|
+
| a | meridiem (lowercase) | am, pm |
|
|
187
|
+
| aa | meridiem (lowercase with ellipsis) | a.m., p.m. |
|
|
188
|
+
| z | time zone name abbreviation | PST, EST |
|
|
189
|
+
| zz | time zone name | Pacific Standard Time |
|
|
189
190
|
|
|
190
191
|
#### Note 1. Comments
|
|
191
192
|
|
package/date-and-time.d.ts
CHANGED
|
@@ -296,7 +296,7 @@ export type Extension = {
|
|
|
296
296
|
export function extend(extension: Extension): void;
|
|
297
297
|
|
|
298
298
|
/** Plugin installer */
|
|
299
|
-
export type Plugin = (proto: unknown,
|
|
299
|
+
export type Plugin = (proto: unknown, date?: unknown) => string;
|
|
300
300
|
|
|
301
301
|
/**
|
|
302
302
|
* Importing plugins
|
package/date-and-time.js
CHANGED
|
@@ -142,7 +142,6 @@
|
|
|
142
142
|
_formatter: _formatter,
|
|
143
143
|
_parser: _parser
|
|
144
144
|
},
|
|
145
|
-
localized_proto,
|
|
146
145
|
date;
|
|
147
146
|
|
|
148
147
|
/**
|
|
@@ -177,6 +176,7 @@
|
|
|
177
176
|
u.getMilliseconds = u.getUTCMilliseconds;
|
|
178
177
|
u.getDay = u.getUTCDay;
|
|
179
178
|
u.getTimezoneOffset = function () { return 0; };
|
|
179
|
+
u.getTimezoneName = function () { return 'UTC'; };
|
|
180
180
|
return u;
|
|
181
181
|
}
|
|
182
182
|
return dateObj;
|
|
@@ -185,7 +185,9 @@
|
|
|
185
185
|
|
|
186
186
|
for (var i = 1, len = pattern.length, token; i < len; i++) {
|
|
187
187
|
token = pattern[i];
|
|
188
|
-
str += formatter[token]
|
|
188
|
+
str += formatter[token]
|
|
189
|
+
? formatter.post(formatter[token](d, pattern[0]))
|
|
190
|
+
: comment.test(token) ? token.replace(comment, '$1') : token;
|
|
189
191
|
}
|
|
190
192
|
return str;
|
|
191
193
|
};
|
|
@@ -206,7 +208,7 @@
|
|
|
206
208
|
dateString = parser.pre(dateString);
|
|
207
209
|
for (var i = 1, len = pattern.length, token, str, result; i < len; i++) {
|
|
208
210
|
token = pattern[i];
|
|
209
|
-
str = dateString.
|
|
211
|
+
str = dateString.substring(dt._index);
|
|
210
212
|
|
|
211
213
|
if (parser[token]) {
|
|
212
214
|
result = parser[token](str, pattern[0]);
|
|
@@ -260,14 +262,13 @@
|
|
|
260
262
|
* @returns {boolean} Whether the date and time string is a valid date and time
|
|
261
263
|
*/
|
|
262
264
|
proto.isValid = function (arg1, arg2) {
|
|
263
|
-
var ctx = this || date, dt = typeof arg1 === 'string' ? ctx.preparse(arg1, arg2) : arg1
|
|
264
|
-
last = [31, 28 + ctx.isLeapYear(dt.Y) | 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][dt.M - 1];
|
|
265
|
+
var ctx = this || date, dt = typeof arg1 === 'string' ? ctx.preparse(arg1, arg2) : arg1;
|
|
265
266
|
|
|
266
267
|
return !(
|
|
267
|
-
dt._index < 1 || dt._length < 1 || dt._index - dt._length || dt._match < 1
|
|
268
|
-
dt.Y < 1 || dt.Y > 9999 || dt.M < 1 || dt.M > 12 || dt.D < 1 || dt.D >
|
|
269
|
-
dt.H < 0 || dt.H > 23 || dt.m < 0 || dt.m > 59 || dt.s < 0 || dt.s > 59 || dt.S < 0 || dt.S > 999
|
|
270
|
-
dt.Z < -840 || dt.Z > 720
|
|
268
|
+
dt._index < 1 || dt._length < 1 || dt._index - dt._length || dt._match < 1
|
|
269
|
+
|| dt.Y < 1 || dt.Y > 9999 || dt.M < 1 || dt.M > 12 || dt.D < 1 || dt.D > new Date(dt.Y, dt.M, 0).getDate()
|
|
270
|
+
|| dt.H < 0 || dt.H > 23 || dt.m < 0 || dt.m > 59 || dt.s < 0 || dt.s > 59 || dt.S < 0 || dt.S > 999
|
|
271
|
+
|| dt.Z < -840 || dt.Z > 720
|
|
271
272
|
);
|
|
272
273
|
};
|
|
273
274
|
|
|
@@ -452,7 +453,6 @@
|
|
|
452
453
|
}
|
|
453
454
|
};
|
|
454
455
|
|
|
455
|
-
localized_proto = extend(proto);
|
|
456
456
|
date = extend(proto);
|
|
457
457
|
|
|
458
458
|
/**
|
|
@@ -473,8 +473,8 @@
|
|
|
473
473
|
var formatter = extend(_formatter, extension.formatter, true, res);
|
|
474
474
|
var parser = extend(_parser, extension.parser, true, res);
|
|
475
475
|
|
|
476
|
-
date._formatter =
|
|
477
|
-
date._parser =
|
|
476
|
+
date._formatter = formatter;
|
|
477
|
+
date._parser = parser;
|
|
478
478
|
|
|
479
479
|
for (var plugin in plugins) {
|
|
480
480
|
date.extend(plugins[plugin]);
|
|
@@ -511,7 +511,7 @@
|
|
|
511
511
|
var install = typeof plugin === 'function' ? plugin : date.plugin[plugin];
|
|
512
512
|
|
|
513
513
|
if (install) {
|
|
514
|
-
date.extend(plugins[install(proto,
|
|
514
|
+
date.extend(plugins[install(proto, date)] || {});
|
|
515
515
|
}
|
|
516
516
|
};
|
|
517
517
|
|
package/date-and-time.min.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).date=t()}(this,(function(){"use strict";
|
|
2
2
|
/**
|
|
3
3
|
* @preserve date-and-time (c) KNOWLEDGECODE | MIT
|
|
4
|
-
*/var e,t
|
|
4
|
+
*/var e,t={},n={},r="en",i={MMMM:["January","February","March","April","May","June","July","August","September","October","November","December"],MMM:["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],dddd:["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],ddd:["Sun","Mon","Tue","Wed","Thu","Fri","Sat"],dd:["Su","Mo","Tu","We","Th","Fr","Sa"],A:["AM","PM"]},u={YYYY:function(e){return("000"+e.getFullYear()).slice(-4)},YY:function(e){return("0"+e.getFullYear()).slice(-2)},Y:function(e){return""+e.getFullYear()},MMMM:function(e){return this.res.MMMM[e.getMonth()]},MMM:function(e){return this.res.MMM[e.getMonth()]},MM:function(e){return("0"+(e.getMonth()+1)).slice(-2)},M:function(e){return""+(e.getMonth()+1)},DD:function(e){return("0"+e.getDate()).slice(-2)},D:function(e){return""+e.getDate()},HH:function(e){return("0"+e.getHours()).slice(-2)},H:function(e){return""+e.getHours()},A:function(e){return this.res.A[e.getHours()>11|0]},hh:function(e){return("0"+(e.getHours()%12||12)).slice(-2)},h:function(e){return""+(e.getHours()%12||12)},mm:function(e){return("0"+e.getMinutes()).slice(-2)},m:function(e){return""+e.getMinutes()},ss:function(e){return("0"+e.getSeconds()).slice(-2)},s:function(e){return""+e.getSeconds()},SSS:function(e){return("00"+e.getMilliseconds()).slice(-3)},SS:function(e){return("0"+(e.getMilliseconds()/10|0)).slice(-2)},S:function(e){return""+(e.getMilliseconds()/100|0)},dddd:function(e){return this.res.dddd[e.getDay()]},ddd:function(e){return this.res.ddd[e.getDay()]},dd:function(e){return this.res.dd[e.getDay()]},Z:function(e){var t=e.getTimezoneOffset()/.6|0;return(t>0?"-":"+")+("000"+Math.abs(t-(t%100*.4|0))).slice(-4)},ZZ:function(e){var t=e.getTimezoneOffset(),n=Math.abs(t);return(t>0?"-":"+")+("0"+(n/60|0)).slice(-2)+":"+("0"+n%60).slice(-2)},post:function(e){return e},res:i},o={YYYY:function(e){return this.exec(/^\d{4}/,e)},Y:function(e){return this.exec(/^\d{1,4}/,e)},MMMM:function(e){var t=this.find(this.res.MMMM,e);return t.value++,t},MMM:function(e){var t=this.find(this.res.MMM,e);return t.value++,t},MM:function(e){return this.exec(/^\d\d/,e)},M:function(e){return this.exec(/^\d\d?/,e)},DD:function(e){return this.exec(/^\d\d/,e)},D:function(e){return this.exec(/^\d\d?/,e)},HH:function(e){return this.exec(/^\d\d/,e)},H:function(e){return this.exec(/^\d\d?/,e)},A:function(e){return this.find(this.res.A,e)},hh:function(e){return this.exec(/^\d\d/,e)},h:function(e){return this.exec(/^\d\d?/,e)},mm:function(e){return this.exec(/^\d\d/,e)},m:function(e){return this.exec(/^\d\d?/,e)},ss:function(e){return this.exec(/^\d\d/,e)},s:function(e){return this.exec(/^\d\d?/,e)},SSS:function(e){return this.exec(/^\d{1,3}/,e)},SS:function(e){var t=this.exec(/^\d\d?/,e);return t.value*=10,t},S:function(e){var t=this.exec(/^\d/,e);return t.value*=100,t},Z:function(e){var t=this.exec(/^[+-]\d{2}[0-5]\d/,e);return t.value=-60*(t.value/100|0)-t.value%100,t},ZZ:function(e){var t=/^([+-])(\d{2}):([0-5]\d)/.exec(e)||["","","",""];return{value:0-(60*(t[1]+t[2]|0)+(t[1]+t[3]|0)),length:t[0].length}},h12:function(e,t){return(12===e?0:e)+12*t},exec:function(e,t){var n=(e.exec(t)||[""])[0];return{value:0|n,length:n.length}},find:function(e,t){for(var n,r=-1,i=0,u=0,o=e.length;u<o;u++)n=e[u],!t.indexOf(n)&&n.length>i&&(r=u,i=n.length);return{value:r,length:i}},pre:function(e){return e},res:i},s=function(e,t,n,r){var i,u={};for(i in e)u[i]=e[i];for(i in t||{})!!n^!!u[i]||(u[i]=t[i]);return r&&(u.res=r),u},c={_formatter:u,_parser:o};return c.compile=function(e){return[e].concat(e.match(/\[(?:[^[\]]|\[[^[\]]*])*]|([A-Za-z])\1*|\.{3}|./g)||[])},c.format=function(t,n,r){for(var i,u=this||e,o="string"==typeof n?u.compile(n):n,s=u._formatter,c=function(){if(r){var e=new Date(t.getTime());return e.getFullYear=e.getUTCFullYear,e.getMonth=e.getUTCMonth,e.getDate=e.getUTCDate,e.getHours=e.getUTCHours,e.getMinutes=e.getUTCMinutes,e.getSeconds=e.getUTCSeconds,e.getMilliseconds=e.getUTCMilliseconds,e.getDay=e.getUTCDay,e.getTimezoneOffset=function(){return 0},e.getTimezoneName=function(){return"UTC"},e}return t}(),a=/^\[(.*)\]$/,d="",f=1,l=o.length;f<l;f++)d+=s[i=o[f]]?s.post(s[i](c,o[0])):a.test(i)?i.replace(a,"$1"):i;return d},c.preparse=function(t,n){var r=this||e,i="string"==typeof n?r.compile(n):n,u=r._parser,o={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},s=/^\[(.*)\]$/;t=u.pre(t);for(var c,a,d,f=1,l=i.length;f<l;f++)if(c=i[f],a=t.substring(o._index),u[c]){if(!(d=u[c](a,i[0])).length)break;o[d.token||c.charAt(0)]=d.value,o._index+=d.length,o._match++}else if(c===a.charAt(0)||" "===c)o._index++;else{if(!s.test(c)||a.indexOf(c.replace(s,"$1"))){if("..."===c){o._index=t.length;break}break}o._index+=c.length-2}return o.H=o.H||u.h12(o.h,o.A),o._length=t.length,o},c.parse=function(t,n,r){var i=this||e,u="string"==typeof n?i.compile(n):n,o=i.preparse(t,u);return i.isValid(o)?(o.M-=o.Y<100?22801:1,r||~i._parser.find(u,"ZZ").value?new Date(Date.UTC(o.Y,o.M,o.D,o.H,o.m+o.Z,o.s,o.S)):new Date(o.Y,o.M,o.D,o.H,o.m,o.s,o.S)):new Date(NaN)},c.isValid=function(t,n){var r="string"==typeof t?(this||e).preparse(t,n):t;return!(r._index<1||r._length<1||r._index-r._length||r._match<1||r.Y<1||r.Y>9999||r.M<1||r.M>12||r.D<1||r.D>new Date(r.Y,r.M,0).getDate()||r.H<0||r.H>23||r.m<0||r.m>59||r.s<0||r.s>59||r.S<0||r.S>999||r.Z<-840||r.Z>720)},c.transform=function(t,n,r,i){const u=this||e;return u.format(u.parse(t,n),r,i)},c.addYears=function(t,n,r){return(this||e).addMonths(t,12*n,r)},c.addMonths=function(t,n,r){var i=new Date(t.getTime());if(r){if(i.setUTCMonth(i.getUTCMonth()+n),i.getUTCDate()<t.getUTCDate())return(this||e).addDays(i,-i.getUTCDate(),r)}else if(i.setMonth(i.getMonth()+n),i.getDate()<t.getDate())return(this||e).addDays(i,-i.getDate(),r);return i},c.addDays=function(t,n,r){return(this||e).addHours(t,24*n,r)},c.addHours=function(t,n,r){return(this||e).addMinutes(t,60*n,r)},c.addMinutes=function(t,n,r){return(this||e).addSeconds(t,60*n,r)},c.addSeconds=function(t,n,r){return(this||e).addMilliseconds(t,1e3*n,r)},c.addMilliseconds=function(e,t,n){var r=new Date(e.getTime());return n?r.setUTCMilliseconds(r.getUTCMilliseconds()+t):r.setMilliseconds(r.getMilliseconds()+t),r},c.subtract=function(e,t){var n=e.getTime()-t.getTime();return{toMilliseconds:function(){return n},toSeconds:function(){return n/1e3},toMinutes:function(){return n/6e4},toHours:function(){return n/36e5},toDays:function(){return n/864e5}}},c.isLeapYear=function(e){return!((e%4||!(e%100))&&e%400)},c.isSameDay=function(e,t){return e.toDateString()===t.toDateString()},c.locale=function(e,n){t[e]||(t[e]=n)},c.plugin=function(e,t){n[e]||(n[e]=t)},(e=s(c)).locale=function(a){var d="function"==typeof a?a:e.locale[a];if(!d)return r;r=d(c);var f=t[r]||{},l=s(i,f.res,!0),h=s(u,f.formatter,!0,l),g=s(o,f.parser,!0,l);for(var M in e._formatter=h,e._parser=g,n)e.extend(n[M]);return r},e.extend=function(t){var n=s(e._parser.res,t.res),r=t.extender||{};for(var i in e._formatter=s(e._formatter,t.formatter,!1,n),e._parser=s(e._parser,t.parser,!1,n),r)e[i]||(e[i]=r[i])},e.plugin=function(t){var r="function"==typeof t?t:e.plugin[t];r&&e.extend(n[r(c,e)]||{})},e}));
|
package/esm/date-and-time.es.js
CHANGED
|
@@ -136,7 +136,6 @@ var locales = {},
|
|
|
136
136
|
_formatter: _formatter,
|
|
137
137
|
_parser: _parser
|
|
138
138
|
},
|
|
139
|
-
localized_proto,
|
|
140
139
|
date;
|
|
141
140
|
|
|
142
141
|
/**
|
|
@@ -171,6 +170,7 @@ proto.format = function (dateObj, arg, utc) {
|
|
|
171
170
|
u.getMilliseconds = u.getUTCMilliseconds;
|
|
172
171
|
u.getDay = u.getUTCDay;
|
|
173
172
|
u.getTimezoneOffset = function () { return 0; };
|
|
173
|
+
u.getTimezoneName = function () { return 'UTC'; };
|
|
174
174
|
return u;
|
|
175
175
|
}
|
|
176
176
|
return dateObj;
|
|
@@ -179,7 +179,9 @@ proto.format = function (dateObj, arg, utc) {
|
|
|
179
179
|
|
|
180
180
|
for (var i = 1, len = pattern.length, token; i < len; i++) {
|
|
181
181
|
token = pattern[i];
|
|
182
|
-
str += formatter[token]
|
|
182
|
+
str += formatter[token]
|
|
183
|
+
? formatter.post(formatter[token](d, pattern[0]))
|
|
184
|
+
: comment.test(token) ? token.replace(comment, '$1') : token;
|
|
183
185
|
}
|
|
184
186
|
return str;
|
|
185
187
|
};
|
|
@@ -200,7 +202,7 @@ proto.preparse = function (dateString, arg) {
|
|
|
200
202
|
dateString = parser.pre(dateString);
|
|
201
203
|
for (var i = 1, len = pattern.length, token, str, result; i < len; i++) {
|
|
202
204
|
token = pattern[i];
|
|
203
|
-
str = dateString.
|
|
205
|
+
str = dateString.substring(dt._index);
|
|
204
206
|
|
|
205
207
|
if (parser[token]) {
|
|
206
208
|
result = parser[token](str, pattern[0]);
|
|
@@ -254,14 +256,13 @@ proto.parse = function (dateString, arg, utc) {
|
|
|
254
256
|
* @returns {boolean} Whether the date and time string is a valid date and time
|
|
255
257
|
*/
|
|
256
258
|
proto.isValid = function (arg1, arg2) {
|
|
257
|
-
var ctx = this || date, dt = typeof arg1 === 'string' ? ctx.preparse(arg1, arg2) : arg1
|
|
258
|
-
last = [31, 28 + ctx.isLeapYear(dt.Y) | 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][dt.M - 1];
|
|
259
|
+
var ctx = this || date, dt = typeof arg1 === 'string' ? ctx.preparse(arg1, arg2) : arg1;
|
|
259
260
|
|
|
260
261
|
return !(
|
|
261
|
-
dt._index < 1 || dt._length < 1 || dt._index - dt._length || dt._match < 1
|
|
262
|
-
dt.Y < 1 || dt.Y > 9999 || dt.M < 1 || dt.M > 12 || dt.D < 1 || dt.D >
|
|
263
|
-
dt.H < 0 || dt.H > 23 || dt.m < 0 || dt.m > 59 || dt.s < 0 || dt.s > 59 || dt.S < 0 || dt.S > 999
|
|
264
|
-
dt.Z < -840 || dt.Z > 720
|
|
262
|
+
dt._index < 1 || dt._length < 1 || dt._index - dt._length || dt._match < 1
|
|
263
|
+
|| dt.Y < 1 || dt.Y > 9999 || dt.M < 1 || dt.M > 12 || dt.D < 1 || dt.D > new Date(dt.Y, dt.M, 0).getDate()
|
|
264
|
+
|| dt.H < 0 || dt.H > 23 || dt.m < 0 || dt.m > 59 || dt.s < 0 || dt.s > 59 || dt.S < 0 || dt.S > 999
|
|
265
|
+
|| dt.Z < -840 || dt.Z > 720
|
|
265
266
|
);
|
|
266
267
|
};
|
|
267
268
|
|
|
@@ -446,7 +447,6 @@ proto.plugin = function (name, plugin) {
|
|
|
446
447
|
}
|
|
447
448
|
};
|
|
448
449
|
|
|
449
|
-
localized_proto = extend(proto);
|
|
450
450
|
date = extend(proto);
|
|
451
451
|
|
|
452
452
|
/**
|
|
@@ -467,8 +467,8 @@ date.locale = function (locale) {
|
|
|
467
467
|
var formatter = extend(_formatter, extension.formatter, true, res);
|
|
468
468
|
var parser = extend(_parser, extension.parser, true, res);
|
|
469
469
|
|
|
470
|
-
date._formatter =
|
|
471
|
-
date._parser =
|
|
470
|
+
date._formatter = formatter;
|
|
471
|
+
date._parser = parser;
|
|
472
472
|
|
|
473
473
|
for (var plugin in plugins) {
|
|
474
474
|
date.extend(plugins[plugin]);
|
|
@@ -505,7 +505,7 @@ date.plugin = function (plugin) {
|
|
|
505
505
|
var install = typeof plugin === 'function' ? plugin : date.plugin[plugin];
|
|
506
506
|
|
|
507
507
|
if (install) {
|
|
508
|
-
date.extend(plugins[install(proto,
|
|
508
|
+
date.extend(plugins[install(proto, date)] || {});
|
|
509
509
|
}
|
|
510
510
|
};
|
|
511
511
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @preserve date-and-time (c) KNOWLEDGECODE | MIT
|
|
3
3
|
*/
|
|
4
|
-
var e,t
|
|
4
|
+
var e,t={},n={},r="en",i={MMMM:["January","February","March","April","May","June","July","August","September","October","November","December"],MMM:["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],dddd:["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],ddd:["Sun","Mon","Tue","Wed","Thu","Fri","Sat"],dd:["Su","Mo","Tu","We","Th","Fr","Sa"],A:["AM","PM"]},u={YYYY:function(e){return("000"+e.getFullYear()).slice(-4)},YY:function(e){return("0"+e.getFullYear()).slice(-2)},Y:function(e){return""+e.getFullYear()},MMMM:function(e){return this.res.MMMM[e.getMonth()]},MMM:function(e){return this.res.MMM[e.getMonth()]},MM:function(e){return("0"+(e.getMonth()+1)).slice(-2)},M:function(e){return""+(e.getMonth()+1)},DD:function(e){return("0"+e.getDate()).slice(-2)},D:function(e){return""+e.getDate()},HH:function(e){return("0"+e.getHours()).slice(-2)},H:function(e){return""+e.getHours()},A:function(e){return this.res.A[e.getHours()>11|0]},hh:function(e){return("0"+(e.getHours()%12||12)).slice(-2)},h:function(e){return""+(e.getHours()%12||12)},mm:function(e){return("0"+e.getMinutes()).slice(-2)},m:function(e){return""+e.getMinutes()},ss:function(e){return("0"+e.getSeconds()).slice(-2)},s:function(e){return""+e.getSeconds()},SSS:function(e){return("00"+e.getMilliseconds()).slice(-3)},SS:function(e){return("0"+(e.getMilliseconds()/10|0)).slice(-2)},S:function(e){return""+(e.getMilliseconds()/100|0)},dddd:function(e){return this.res.dddd[e.getDay()]},ddd:function(e){return this.res.ddd[e.getDay()]},dd:function(e){return this.res.dd[e.getDay()]},Z:function(e){var t=e.getTimezoneOffset()/.6|0;return(t>0?"-":"+")+("000"+Math.abs(t-(t%100*.4|0))).slice(-4)},ZZ:function(e){var t=e.getTimezoneOffset(),n=Math.abs(t);return(t>0?"-":"+")+("0"+(n/60|0)).slice(-2)+":"+("0"+n%60).slice(-2)},post:function(e){return e},res:i},s={YYYY:function(e){return this.exec(/^\d{4}/,e)},Y:function(e){return this.exec(/^\d{1,4}/,e)},MMMM:function(e){var t=this.find(this.res.MMMM,e);return t.value++,t},MMM:function(e){var t=this.find(this.res.MMM,e);return t.value++,t},MM:function(e){return this.exec(/^\d\d/,e)},M:function(e){return this.exec(/^\d\d?/,e)},DD:function(e){return this.exec(/^\d\d/,e)},D:function(e){return this.exec(/^\d\d?/,e)},HH:function(e){return this.exec(/^\d\d/,e)},H:function(e){return this.exec(/^\d\d?/,e)},A:function(e){return this.find(this.res.A,e)},hh:function(e){return this.exec(/^\d\d/,e)},h:function(e){return this.exec(/^\d\d?/,e)},mm:function(e){return this.exec(/^\d\d/,e)},m:function(e){return this.exec(/^\d\d?/,e)},ss:function(e){return this.exec(/^\d\d/,e)},s:function(e){return this.exec(/^\d\d?/,e)},SSS:function(e){return this.exec(/^\d{1,3}/,e)},SS:function(e){var t=this.exec(/^\d\d?/,e);return t.value*=10,t},S:function(e){var t=this.exec(/^\d/,e);return t.value*=100,t},Z:function(e){var t=this.exec(/^[+-]\d{2}[0-5]\d/,e);return t.value=-60*(t.value/100|0)-t.value%100,t},ZZ:function(e){var t=/^([+-])(\d{2}):([0-5]\d)/.exec(e)||["","","",""];return{value:0-(60*(t[1]+t[2]|0)+(t[1]+t[3]|0)),length:t[0].length}},h12:function(e,t){return(12===e?0:e)+12*t},exec:function(e,t){var n=(e.exec(t)||[""])[0];return{value:0|n,length:n.length}},find:function(e,t){for(var n,r=-1,i=0,u=0,s=e.length;u<s;u++)n=e[u],!t.indexOf(n)&&n.length>i&&(r=u,i=n.length);return{value:r,length:i}},pre:function(e){return e},res:i},o=function(e,t,n,r){var i,u={};for(i in e)u[i]=e[i];for(i in t||{})!!n^!!u[i]||(u[i]=t[i]);return r&&(u.res=r),u},a={_formatter:u,_parser:s};a.compile=function(e){return[e].concat(e.match(/\[(?:[^[\]]|\[[^[\]]*])*]|([A-Za-z])\1*|\.{3}|./g)||[])},a.format=function(t,n,r){for(var i,u=this||e,s="string"==typeof n?u.compile(n):n,o=u._formatter,a=function(){if(r){var e=new Date(t.getTime());return e.getFullYear=e.getUTCFullYear,e.getMonth=e.getUTCMonth,e.getDate=e.getUTCDate,e.getHours=e.getUTCHours,e.getMinutes=e.getUTCMinutes,e.getSeconds=e.getUTCSeconds,e.getMilliseconds=e.getUTCMilliseconds,e.getDay=e.getUTCDay,e.getTimezoneOffset=function(){return 0},e.getTimezoneName=function(){return"UTC"},e}return t}(),c=/^\[(.*)\]$/,d="",f=1,l=s.length;f<l;f++)d+=o[i=s[f]]?o.post(o[i](a,s[0])):c.test(i)?i.replace(c,"$1"):i;return d},a.preparse=function(t,n){var r=this||e,i="string"==typeof n?r.compile(n):n,u=r._parser,s={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},o=/^\[(.*)\]$/;t=u.pre(t);for(var a,c,d,f=1,l=i.length;f<l;f++)if(a=i[f],c=t.substring(s._index),u[a]){if(!(d=u[a](c,i[0])).length)break;s[d.token||a.charAt(0)]=d.value,s._index+=d.length,s._match++}else if(a===c.charAt(0)||" "===a)s._index++;else{if(!o.test(a)||c.indexOf(a.replace(o,"$1"))){if("..."===a){s._index=t.length;break}break}s._index+=a.length-2}return s.H=s.H||u.h12(s.h,s.A),s._length=t.length,s},a.parse=function(t,n,r){var i=this||e,u="string"==typeof n?i.compile(n):n,s=i.preparse(t,u);return i.isValid(s)?(s.M-=s.Y<100?22801:1,r||~i._parser.find(u,"ZZ").value?new Date(Date.UTC(s.Y,s.M,s.D,s.H,s.m+s.Z,s.s,s.S)):new Date(s.Y,s.M,s.D,s.H,s.m,s.s,s.S)):new Date(NaN)},a.isValid=function(t,n){var r="string"==typeof t?(this||e).preparse(t,n):t;return!(r._index<1||r._length<1||r._index-r._length||r._match<1||r.Y<1||r.Y>9999||r.M<1||r.M>12||r.D<1||r.D>new Date(r.Y,r.M,0).getDate()||r.H<0||r.H>23||r.m<0||r.m>59||r.s<0||r.s>59||r.S<0||r.S>999||r.Z<-840||r.Z>720)},a.transform=function(t,n,r,i){const u=this||e;return u.format(u.parse(t,n),r,i)},a.addYears=function(t,n,r){return(this||e).addMonths(t,12*n,r)},a.addMonths=function(t,n,r){var i=new Date(t.getTime());if(r){if(i.setUTCMonth(i.getUTCMonth()+n),i.getUTCDate()<t.getUTCDate())return(this||e).addDays(i,-i.getUTCDate(),r)}else if(i.setMonth(i.getMonth()+n),i.getDate()<t.getDate())return(this||e).addDays(i,-i.getDate(),r);return i},a.addDays=function(t,n,r){return(this||e).addHours(t,24*n,r)},a.addHours=function(t,n,r){return(this||e).addMinutes(t,60*n,r)},a.addMinutes=function(t,n,r){return(this||e).addSeconds(t,60*n,r)},a.addSeconds=function(t,n,r){return(this||e).addMilliseconds(t,1e3*n,r)},a.addMilliseconds=function(e,t,n){var r=new Date(e.getTime());return n?r.setUTCMilliseconds(r.getUTCMilliseconds()+t):r.setMilliseconds(r.getMilliseconds()+t),r},a.subtract=function(e,t){var n=e.getTime()-t.getTime();return{toMilliseconds:function(){return n},toSeconds:function(){return n/1e3},toMinutes:function(){return n/6e4},toHours:function(){return n/36e5},toDays:function(){return n/864e5}}},a.isLeapYear=function(e){return!((e%4||!(e%100))&&e%400)},a.isSameDay=function(e,t){return e.toDateString()===t.toDateString()},a.locale=function(e,n){t[e]||(t[e]=n)},a.plugin=function(e,t){n[e]||(n[e]=t)},(e=o(a)).locale=function(c){var d="function"==typeof c?c:e.locale[c];if(!d)return r;r=d(a);var f=t[r]||{},l=o(i,f.res,!0),h=o(u,f.formatter,!0,l),g=o(s,f.parser,!0,l);for(var M in e._formatter=h,e._parser=g,n)e.extend(n[M]);return r},e.extend=function(t){var n=o(e._parser.res,t.res),r=t.extender||{};for(var i in e._formatter=o(e._formatter,t.formatter,!1,n),e._parser=o(e._parser,t.parser,!1,n),r)e[i]||(e[i]=r[i])},e.plugin=function(t){var r="function"==typeof t?t:e.plugin[t];r&&e.extend(n[r(a,e)]||{})};var c=e;export{c as default};
|
package/esm/date-and-time.mjs
CHANGED
|
@@ -136,7 +136,6 @@ var locales = {},
|
|
|
136
136
|
_formatter: _formatter,
|
|
137
137
|
_parser: _parser
|
|
138
138
|
},
|
|
139
|
-
localized_proto,
|
|
140
139
|
date;
|
|
141
140
|
|
|
142
141
|
/**
|
|
@@ -171,6 +170,7 @@ proto.format = function (dateObj, arg, utc) {
|
|
|
171
170
|
u.getMilliseconds = u.getUTCMilliseconds;
|
|
172
171
|
u.getDay = u.getUTCDay;
|
|
173
172
|
u.getTimezoneOffset = function () { return 0; };
|
|
173
|
+
u.getTimezoneName = function () { return 'UTC'; };
|
|
174
174
|
return u;
|
|
175
175
|
}
|
|
176
176
|
return dateObj;
|
|
@@ -179,7 +179,9 @@ proto.format = function (dateObj, arg, utc) {
|
|
|
179
179
|
|
|
180
180
|
for (var i = 1, len = pattern.length, token; i < len; i++) {
|
|
181
181
|
token = pattern[i];
|
|
182
|
-
str += formatter[token]
|
|
182
|
+
str += formatter[token]
|
|
183
|
+
? formatter.post(formatter[token](d, pattern[0]))
|
|
184
|
+
: comment.test(token) ? token.replace(comment, '$1') : token;
|
|
183
185
|
}
|
|
184
186
|
return str;
|
|
185
187
|
};
|
|
@@ -200,7 +202,7 @@ proto.preparse = function (dateString, arg) {
|
|
|
200
202
|
dateString = parser.pre(dateString);
|
|
201
203
|
for (var i = 1, len = pattern.length, token, str, result; i < len; i++) {
|
|
202
204
|
token = pattern[i];
|
|
203
|
-
str = dateString.
|
|
205
|
+
str = dateString.substring(dt._index);
|
|
204
206
|
|
|
205
207
|
if (parser[token]) {
|
|
206
208
|
result = parser[token](str, pattern[0]);
|
|
@@ -254,14 +256,13 @@ proto.parse = function (dateString, arg, utc) {
|
|
|
254
256
|
* @returns {boolean} Whether the date and time string is a valid date and time
|
|
255
257
|
*/
|
|
256
258
|
proto.isValid = function (arg1, arg2) {
|
|
257
|
-
var ctx = this || date, dt = typeof arg1 === 'string' ? ctx.preparse(arg1, arg2) : arg1
|
|
258
|
-
last = [31, 28 + ctx.isLeapYear(dt.Y) | 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][dt.M - 1];
|
|
259
|
+
var ctx = this || date, dt = typeof arg1 === 'string' ? ctx.preparse(arg1, arg2) : arg1;
|
|
259
260
|
|
|
260
261
|
return !(
|
|
261
|
-
dt._index < 1 || dt._length < 1 || dt._index - dt._length || dt._match < 1
|
|
262
|
-
dt.Y < 1 || dt.Y > 9999 || dt.M < 1 || dt.M > 12 || dt.D < 1 || dt.D >
|
|
263
|
-
dt.H < 0 || dt.H > 23 || dt.m < 0 || dt.m > 59 || dt.s < 0 || dt.s > 59 || dt.S < 0 || dt.S > 999
|
|
264
|
-
dt.Z < -840 || dt.Z > 720
|
|
262
|
+
dt._index < 1 || dt._length < 1 || dt._index - dt._length || dt._match < 1
|
|
263
|
+
|| dt.Y < 1 || dt.Y > 9999 || dt.M < 1 || dt.M > 12 || dt.D < 1 || dt.D > new Date(dt.Y, dt.M, 0).getDate()
|
|
264
|
+
|| dt.H < 0 || dt.H > 23 || dt.m < 0 || dt.m > 59 || dt.s < 0 || dt.s > 59 || dt.S < 0 || dt.S > 999
|
|
265
|
+
|| dt.Z < -840 || dt.Z > 720
|
|
265
266
|
);
|
|
266
267
|
};
|
|
267
268
|
|
|
@@ -446,7 +447,6 @@ proto.plugin = function (name, plugin) {
|
|
|
446
447
|
}
|
|
447
448
|
};
|
|
448
449
|
|
|
449
|
-
localized_proto = extend(proto);
|
|
450
450
|
date = extend(proto);
|
|
451
451
|
|
|
452
452
|
/**
|
|
@@ -467,8 +467,8 @@ date.locale = function (locale) {
|
|
|
467
467
|
var formatter = extend(_formatter, extension.formatter, true, res);
|
|
468
468
|
var parser = extend(_parser, extension.parser, true, res);
|
|
469
469
|
|
|
470
|
-
date._formatter =
|
|
471
|
-
date._parser =
|
|
470
|
+
date._formatter = formatter;
|
|
471
|
+
date._parser = parser;
|
|
472
472
|
|
|
473
473
|
for (var plugin in plugins) {
|
|
474
474
|
date.extend(plugins[plugin]);
|
|
@@ -505,7 +505,7 @@ date.plugin = function (plugin) {
|
|
|
505
505
|
var install = typeof plugin === 'function' ? plugin : date.plugin[plugin];
|
|
506
506
|
|
|
507
507
|
if (install) {
|
|
508
|
-
date.extend(plugins[install(proto,
|
|
508
|
+
date.extend(plugins[install(proto, date)] || {});
|
|
509
509
|
}
|
|
510
510
|
};
|
|
511
511
|
|