date-and-time 2.1.2 → 2.2.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 +25 -9
- package/README.md +10 -18
- package/date-and-time.js +9 -0
- package/date-and-time.min.js +11 -10
- package/esm/date-and-time.es.js +9 -0
- package/esm/date-and-time.es.min.js +5 -5
- package/esm/date-and-time.mjs +9 -0
- package/esm/plugin/timezone.es.js +5 -1
- package/esm/plugin/timezone.mjs +5 -1
- package/package.json +3 -3
- package/plugin/timezone.js +5 -1
package/PLUGINS.md
CHANGED
|
@@ -76,7 +76,7 @@ date.plugin('foobar');
|
|
|
76
76
|
- It adds `timeSpan()` function that calculates the difference of two dates to the library.
|
|
77
77
|
|
|
78
78
|
- [timezone](#timezone)
|
|
79
|
-
- It adds `formatTZ()` and `
|
|
79
|
+
- It adds `formatTZ()`, `parseTZ()` and `transformTZ()` that support `IANA time zone names` to the library.
|
|
80
80
|
|
|
81
81
|
- [two-digit-year](#two-digit-year)
|
|
82
82
|
- It adds two-digit year notation to the parser.
|
|
@@ -290,7 +290,7 @@ In these functions can be available some tokens to format the calculation result
|
|
|
290
290
|
|
|
291
291
|
### timezone
|
|
292
292
|
|
|
293
|
-
It adds `formatTZ()` and `
|
|
293
|
+
It adds `formatTZ()`, `parseTZ()` and `transformTZ()` that support `IANA time zone names` (`America/Los_Angeles`, `Asia/Tokyo`, and so on) to the library.
|
|
294
294
|
|
|
295
295
|
#### formatTZ(dateObj, arg[, timeZone])
|
|
296
296
|
|
|
@@ -299,7 +299,7 @@ It adds `formatTZ()` and `parseTZ()` functions that support `IANA time zone name
|
|
|
299
299
|
- @param {**string**} [timeZone] - output as this time zone
|
|
300
300
|
- @returns {**string**} a formatted string
|
|
301
301
|
|
|
302
|
-
|
|
302
|
+
`formatTZ()` is upward compatible with `format()`. Tokens available for `arg` are the same as those for `format()`. If `timeZone` is omitted, this function assumes `timeZone` to be a local time zone and outputs a string. This means that the result is the same as when `format()` is used.
|
|
303
303
|
|
|
304
304
|
#### parseTZ(dateString, arg[, timeZone])
|
|
305
305
|
|
|
@@ -308,7 +308,17 @@ The `formatTZ()` is upward compatible with `format()`. Tokens available here are
|
|
|
308
308
|
- @param {**string**} [timeZone] - input as this time zone
|
|
309
309
|
- @returns {**Date**} a constructed date
|
|
310
310
|
|
|
311
|
-
|
|
311
|
+
`parseTZ()` is upward compatible with `parse()`. Tokens available for `arg` are the same as those for `parse()`. `timeZone` in this function is used as supplemental information. if `dateString` contains a time zone offset value (i.e. -0800, +0900), `timeZone` is not be used. If `dateString` doesn't contain a time zone offset value and `timeZone` is omitted, this function assumes `timeZone` to be a local time zone. This means that the result is the same as when `parse()` is used.
|
|
312
|
+
|
|
313
|
+
#### transformTZ(dateString, arg1, arg2[, timeZone])
|
|
314
|
+
|
|
315
|
+
- @param {**string**} dateString - a date string
|
|
316
|
+
- @param {**string|Array.\<string\>**} arg1 - a format string or its compiled object
|
|
317
|
+
- @param {**string|Array.\<string\>**} arg2 - a transformed format string or its compiled object
|
|
318
|
+
- @param {**string**} [timeZone] - output as this time zone
|
|
319
|
+
- @returns {**string**} a formatted string
|
|
320
|
+
|
|
321
|
+
`transformTZ()` is upward compatible with `transform()`. `dateString` must itself contain a time zone offset value (i.e. -0800, +0900), otherwise this function assumes it is a local time zone. Tokens available for `arg1` are the same as those for `parse()`, also tokens available for `arg2` are the same as those for `format()`. `timeZone` is a `IANA time zone names`, which is required to output a new formatted string. If it is omitted, this function assumes `timeZone` to be a local time zone. This means that the result is the same as when `transform()` is used.
|
|
312
322
|
|
|
313
323
|
```javascript
|
|
314
324
|
const date = require('date-and-time');
|
|
@@ -329,16 +339,22 @@ date.parseTZ('Sep 25 2021 4:00:00', 'MMM D YYYY H:mm:ss', 'Pacific/Honolulu');
|
|
|
329
339
|
|
|
330
340
|
// Parses the date string assuming that the time zone is "Europe/London" (UTC+0100).
|
|
331
341
|
date.parseTZ('Sep 25 2021 4:00:00', 'MMM D YYYY H:mm:ss', 'Europe/London'); // 2021-09-25T03:00:00.000Z
|
|
342
|
+
|
|
343
|
+
// Transforms the date string from EST (Eastern Standard Time) to PDT (Pacific Daylight Time).
|
|
344
|
+
date.transformTZ('2021-11-07T03:59:59 UTC-0500', 'YYYY-MM-DD[T]HH:mm:ss [UTC]Z', 'MMMM D YYYY H:mm:ss UTC[Z]', 'America/Los_Angeles'); // November 7 2021 1:59:59 UTC-0700
|
|
345
|
+
|
|
346
|
+
// Transforms the date string from PDT(Pacific Daylight Time) to JST (Japan Standard Time).
|
|
347
|
+
date.transformTZ('2021-03-14T03:00:00 UTC-0700', 'YYYY-MM-DD[T]HH:mm:ss [UTC]Z', 'MMMM D YYYY H:mm:ss UTC[Z]', 'Asia/Tokyo'); // March 14 2021 19:00:00 UTC+0900
|
|
332
348
|
```
|
|
333
349
|
|
|
334
350
|
#### Caveats
|
|
335
351
|
|
|
336
352
|
- This plugin uses the [Intl](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl) object to parse `IANA time zone names`. Note that if you use this plugin in older browsers, this may **NOT** be supported there. At least it does not work in IE.
|
|
337
|
-
- If you don't need to use `IANA time zone names`, you should not use this plugin for performance reasons.
|
|
353
|
+
- If you don't need to use `IANA time zone names`, you should not use this plugin for performance reasons. Recommended to use `format()` and `parse()`.
|
|
338
354
|
|
|
339
355
|
#### Start of DST (Daylight Saving Time)
|
|
340
356
|
|
|
341
|
-
For example, in the US, when local standard time is about to reach Sunday, 14 March 2021, `02:00:00` clocks are turned `forward` 1 hour to Sunday, 14 March 2021, `03:00:00` local daylight time instead. Thus
|
|
357
|
+
For example, in the US, when local standard time is about to reach Sunday, 14 March 2021, `02:00:00` clocks are turned `forward` 1 hour to Sunday, 14 March 2021, `03:00:00` local daylight time instead. Thus there is no `02:00:00` to `02:59:59` on 14 March 2021. In such edge cases, `parseTZ()` will parse like this:
|
|
342
358
|
|
|
343
359
|
```javascript
|
|
344
360
|
date.parseTZ('Mar 14 2021 1:59:59', 'MMM D YYYY H:mm:ss', 'America/Los_Angeles'); // => 2021-03-14T09:59:59Z
|
|
@@ -349,16 +365,16 @@ date.parseTZ('Mar 14 2021 3:00:00', 'MMM D YYYY H:mm:ss', 'America/Los_Angeles')
|
|
|
349
365
|
|
|
350
366
|
#### End of DST
|
|
351
367
|
|
|
352
|
-
Also, when local daylight time is about to reach Sunday, 7 November 2021, `02:00:00` clocks are turned `backward` 1 hour to Sunday, 7 November 2021, `01:00:00` local standard time instead. Thus
|
|
368
|
+
Also, when local daylight time is about to reach Sunday, 7 November 2021, `02:00:00` clocks are turned `backward` 1 hour to Sunday, 7 November 2021, `01:00:00` local standard time instead. Thus `01:00:00` to `01:59:59` on November 7 2021 is repeated twice. Since there are two possible times here, `parseTZ()` assumes that the time is the former (DST) in order to make the result unique:
|
|
353
369
|
|
|
354
370
|
```javascript
|
|
355
|
-
// The parseTZ() assumes that
|
|
371
|
+
// This time is DST or PST? The parseTZ() always assumes that it is DST.
|
|
356
372
|
date.parseTZ('Nov 7 2021 1:59:59', 'MMM D YYYY H:mm:ss', 'America/Los_Angeles'); // => 2021-11-07T08:59:59Z
|
|
357
373
|
// This time is already PST.
|
|
358
374
|
date.parseTZ('Nov 7 2021 2:00:00', 'MMM D YYYY H:mm:ss', 'America/Los_Angeles'); // => 2021-11-07T10:00:00Z
|
|
359
375
|
```
|
|
360
376
|
|
|
361
|
-
|
|
377
|
+
In the first example above, if you want `parseTZ()` to parse the time as PST, you need to pass a date string containing the time zone offset value. In this case, it is better to use `parse()` instead:
|
|
362
378
|
|
|
363
379
|
```javascript
|
|
364
380
|
date.parse('Nov 7 2021 1:59:59 -0800', 'MMM D YYYY H:mm:ss Z'); // => 2021-11-07T09:59:59Z
|
package/README.md
CHANGED
|
@@ -24,26 +24,16 @@ npm i date-and-time
|
|
|
24
24
|
|
|
25
25
|
## Recent Changes
|
|
26
26
|
|
|
27
|
+
- 2.2.0
|
|
28
|
+
- Added `tranformTZ()` to `timezone` plugin. See [PLUGINS.md](./PLUGINS.md) for details.
|
|
29
|
+
- Added `ZZ` token to supports time zone values like `-08:00` `+09:00` to `format()` and `parse()`.
|
|
30
|
+
|
|
27
31
|
- 2.1.2
|
|
28
|
-
- Fixed an issue that the lib's validation logic would
|
|
32
|
+
- Fixed an issue that the lib's validation logic would consider an error when a time zone offset value of a date-time string was greater than +12 hours.
|
|
29
33
|
|
|
30
34
|
- 2.1.1
|
|
31
35
|
- Updated dev dependencies to resolve vulnerability.
|
|
32
36
|
|
|
33
|
-
- 2.1.0
|
|
34
|
-
- Fixed an issue that the lib's functions assigned to variables using ES6 destructuring assignment cause an error.
|
|
35
|
-
|
|
36
|
-
```javascript
|
|
37
|
-
// Destructuring assignment
|
|
38
|
-
const { format, parse } = require('date-and-time');
|
|
39
|
-
|
|
40
|
-
// These used to be errors in 2.0.x.
|
|
41
|
-
format(new Date(), 'MMM DD YYYY');
|
|
42
|
-
parse('Jan 11 2022', 'MMM DD YYYY');
|
|
43
|
-
```
|
|
44
|
-
|
|
45
|
-
- Added Swedish support.
|
|
46
|
-
|
|
47
37
|
## Usage
|
|
48
38
|
|
|
49
39
|
- ES Modules:
|
|
@@ -181,7 +171,8 @@ Available tokens and their meanings are as follows:
|
|
|
181
171
|
| SSS | millisecond (high accuracy) | 753, 022 |
|
|
182
172
|
| SS | millisecond (middle accuracy) | 75, 02 |
|
|
183
173
|
| S | millisecond (low accuracy) | 7, 0 |
|
|
184
|
-
| Z |
|
|
174
|
+
| Z | time zone offset value | +0100, -0800 |
|
|
175
|
+
| ZZ | time zone offset value with colon | +01:00, -08:00 |
|
|
185
176
|
|
|
186
177
|
You can also use the following tokens by importing plugins. See [PLUGINS.md](./PLUGINS.md) for details.
|
|
187
178
|
|
|
@@ -255,7 +246,8 @@ Available tokens and their meanings are as follows:
|
|
|
255
246
|
| SSS | millisecond (high accuracy) | 753, 022 |
|
|
256
247
|
| SS | millisecond (middle accuracy) | 75, 02 |
|
|
257
248
|
| S | millisecond (low accuracy) | 7, 0 |
|
|
258
|
-
| Z |
|
|
249
|
+
| Z | time zone offset value | +0100, -0800 |
|
|
250
|
+
| ZZ | time zone offset value with colon | +01:00, -08:00 |
|
|
259
251
|
|
|
260
252
|
You can also use the following tokens by importing plugins. See [PLUGINS.md](./PLUGINS.md) for details.
|
|
261
253
|
|
|
@@ -286,7 +278,7 @@ if (isNaN(today)) {
|
|
|
286
278
|
|
|
287
279
|
#### Note 2. Input as UTC
|
|
288
280
|
|
|
289
|
-
This function
|
|
281
|
+
This function assumes the `dateString` is a local date-time unless it contains a time zone offset value. Set to true the `utc` option (the 3rd parameter) if it is a UTC date-time.
|
|
290
282
|
|
|
291
283
|
```javascript
|
|
292
284
|
date.parse('11:14:05 PM', 'hh:mm:ss A'); // => Jan 1 1970 23:14:05 GMT-0800
|
package/date-and-time.js
CHANGED
|
@@ -48,6 +48,11 @@
|
|
|
48
48
|
var offset = d.getTimezoneOffset() / 0.6 | 0;
|
|
49
49
|
return (offset > 0 ? '-' : '+') + ('000' + Math.abs(offset - (offset % 100 * 0.4 | 0))).slice(-4);
|
|
50
50
|
},
|
|
51
|
+
ZZ: function (d/*, formatString*/) {
|
|
52
|
+
var offset = d.getTimezoneOffset();
|
|
53
|
+
var mod = Math.abs(offset);
|
|
54
|
+
return (offset > 0 ? '-' : '+') + ('0' + (mod / 60 | 0)).slice(-2) + ':' + ('0' + mod % 60).slice(-2);
|
|
55
|
+
},
|
|
51
56
|
post: function (str) { return str; },
|
|
52
57
|
res: _res
|
|
53
58
|
},
|
|
@@ -93,6 +98,10 @@
|
|
|
93
98
|
result.value = (result.value / 100 | 0) * -60 - result.value % 100;
|
|
94
99
|
return result;
|
|
95
100
|
},
|
|
101
|
+
ZZ: function (str/*, formatString */) {
|
|
102
|
+
var arr = /^([\+-])(\d{2}):([0-5]\d)/.exec(str) || ['', '', '', ''];
|
|
103
|
+
return { value: 0 - ((arr[1] + arr[2] | 0) * 60 + (arr[1] + arr[3] | 0)), length: arr[0].length };
|
|
104
|
+
},
|
|
96
105
|
h12: function (h, a) { return (h === 12 ? 0 : h) + a * 12; },
|
|
97
106
|
exec: function (re, str) {
|
|
98
107
|
var result = (re.exec(str) || [''])[0];
|
package/date-and-time.min.js
CHANGED
|
@@ -4,13 +4,14 @@
|
|
|
4
4
|
'use strict';(function(p,m){"object"===typeof exports&&"undefined"!==typeof module?module.exports=m():"function"===typeof define&&define.amd?define(m):(p="undefined"!==typeof globalThis?globalThis:p||self,p.date=m())})(this,function(){var p={},m={},r="en",t={MMMM:"January February March April May June July August September October November December".split(" "),MMM:"Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec".split(" "),dddd:"Sunday Monday Tuesday Wednesday Thursday Friday Saturday".split(" "),
|
|
5
5
|
ddd:"Sun Mon Tue Wed Thu Fri Sat".split(" "),dd:"Su Mo Tu We Th Fr Sa".split(" "),A:["AM","PM"]},w={YYYY:function(a){return("000"+a.getFullYear()).slice(-4)},YY:function(a){return("0"+a.getFullYear()).slice(-2)},Y:function(a){return""+a.getFullYear()},MMMM:function(a){return this.res.MMMM[a.getMonth()]},MMM:function(a){return this.res.MMM[a.getMonth()]},MM:function(a){return("0"+(a.getMonth()+1)).slice(-2)},M:function(a){return""+(a.getMonth()+1)},DD:function(a){return("0"+a.getDate()).slice(-2)},
|
|
6
6
|
D:function(a){return""+a.getDate()},HH:function(a){return("0"+a.getHours()).slice(-2)},H:function(a){return""+a.getHours()},A:function(a){return this.res.A[11<a.getHours()|0]},hh:function(a){return("0"+(a.getHours()%12||12)).slice(-2)},h:function(a){return""+(a.getHours()%12||12)},mm:function(a){return("0"+a.getMinutes()).slice(-2)},m:function(a){return""+a.getMinutes()},ss:function(a){return("0"+a.getSeconds()).slice(-2)},s:function(a){return""+a.getSeconds()},SSS:function(a){return("00"+a.getMilliseconds()).slice(-3)},
|
|
7
|
-
SS:function(a){return("0"+(a.getMilliseconds()/10|0)).slice(-2)},S:function(a){return""+(a.getMilliseconds()/100|0)},dddd:function(a){return this.res.dddd[a.getDay()]},ddd:function(a){return this.res.ddd[a.getDay()]},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)},
|
|
8
|
-
MMMM:function(a){a=this.find(this.res.MMMM,a);a.value++;return a},MMM:function(a){a=this.find(this.res.MMM,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,
|
|
9
|
-
a)},mm:function(a){return this.exec(/^\d\d/,a)},m:function(a){return this.exec(/^\d\d?/,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/
|
|
10
|
-
b){a=(a.exec(b)||[""])[0];return{value:a|0,length:a.length}},find:function(a,b){for(var c=-1,d=0,h=0,g=a.length,l;h<g;h++)l=a[h],!b.indexOf(l)&&l.length>d&&(c=h,d=l.length);return{value:c,length:d}},pre:function(a){return a},res:t},n=function(a,b,c,d){var h={},g;for(g in a)h[g]=
|
|
11
|
-
f.format=function(a,b,c){var d=this||e;b="string"===typeof b?d.compile(b):b;var h=a.getTimezoneOffset();a=d.addMinutes(a,c?h:0);d=d._formatter;var g="";a.getTimezoneOffset=function(){return c?0:h};for(var l=1,u=b.length,k;l<u;l++)k=b[l],g+=d[k]?d.post(d[k](a,b[0])):k.replace(/\[(.*)]/,
|
|
12
|
-
1,u=b.length,k,q;l<u;l++)if(k=b[l],c[k]){q=c[k](a.slice(g),b[0]);if(!q.length)break;g+=q.length;d[q.token||k.charAt(0)]=q.value;d._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}d.H=
|
|
13
|
-
a.M,a.D,a.H,a.m,a.s,a.S)):new Date(NaN)};f.isValid=function(a,b){var c=this||e;a="string"===typeof a?c.preparse(a,b):a;c=[31,28+c.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||
|
|
14
|
-
12*b)};f.addMonths=function(a,b){a=new Date(a.getTime());a.setMonth(a.getMonth()+b);return a};f.addDays=function(a,b){a=new Date(a.getTime());a.setDate(a.getDate()+b);return a};f.addHours=function(a,b){return(this||e).addMinutes(a,60*b)};f.addMinutes=function(a,b){return(this||
|
|
15
|
-
toSeconds:function(){return c/1E3},toMinutes:function(){return c/6E4},toHours:function(){return c/36E5},toDays:function(){return c/864E5}}};f.isLeapYear=function(a){return!(a%4)&&!!(a%100)||!(a%400)};f.isSameDay=function(a,b){return a.toDateString()===
|
|
16
|
-
c);b=n(x,b.parser,!0,c);e._formatter=v._formatter=a;e._parser=v._parser=b;for(var d in m)e.extend(m[d]);return r};e.extend=function(a){var b=n(e._parser.res,a.res),c=a.extender||{};e._formatter=n(e._formatter,a.formatter,!1,b);e._parser=n(e._parser,a.parser
|
|
7
|
+
SS:function(a){return("0"+(a.getMilliseconds()/10|0)).slice(-2)},S:function(a){return""+(a.getMilliseconds()/100|0)},dddd:function(a){return this.res.dddd[a.getDay()]},ddd:function(a){return this.res.ddd[a.getDay()]},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},
|
|
8
|
+
res:t},x={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,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,
|
|
9
|
+
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?/,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/
|
|
10
|
+
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,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 c=-1,d=0,h=0,g=a.length,l;h<g;h++)l=a[h],!b.indexOf(l)&&l.length>d&&(c=h,d=l.length);return{value:c,length:d}},pre:function(a){return a},res:t},n=function(a,b,c,d){var h={},g;for(g in a)h[g]=
|
|
11
|
+
a[g];for(g in b||{})!!c^!!h[g]||(h[g]=b[g]);d&&(h.res=d);return h},f={_formatter:w,_parser:x};f.compile=function(a){for(var b=/\[([^\[\]]|\[[^\[\]]*])*]|([A-Za-z])\2+|\.{3}|./g,c,d=[a];c=b.exec(a);)d[d.length]=c[0];return d};f.format=function(a,b,c){var d=this||e;b="string"===typeof b?d.compile(b):b;var h=a.getTimezoneOffset();a=d.addMinutes(a,c?h:0);d=d._formatter;var g="";a.getTimezoneOffset=function(){return c?0:h};for(var l=1,u=b.length,k;l<u;l++)k=b[l],g+=d[k]?d.post(d[k](a,b[0])):k.replace(/\[(.*)]/,
|
|
12
|
+
"$1");return g};f.preparse=function(a,b){var c=this||e;b="string"===typeof b?c.compile(b):b;var d={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=/\[(.*)]/;c=c._parser;var g=0;a=c.pre(a);for(var l=1,u=b.length,k,q;l<u;l++)if(k=b[l],c[k]){q=c[k](a.slice(g),b[0]);if(!q.length)break;g+=q.length;d[q.token||k.charAt(0)]=q.value;d._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}d.H=
|
|
13
|
+
d.H||c.h12(d.h,d.A);d._index=g;d._length=a.length;return d};f.parse=function(a,b,c){var d=this||e;a=d.preparse(a,b);return d.isValid(a)?(a.M-=100>a.Y?22801:1,c||a.Z?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)};f.isValid=function(a,b){var c=this||e;a="string"===typeof a?c.preparse(a,b):a;c=[31,28+c.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||
|
|
14
|
+
12<a.M||1>a.D||a.D>c||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)};f.transform=function(a,b,c,d){let h=this||e;return h.format(h.parse(a,b),c,d)};f.addYears=function(a,b){return(this||e).addMonths(a,12*b)};f.addMonths=function(a,b){a=new Date(a.getTime());a.setMonth(a.getMonth()+b);return a};f.addDays=function(a,b){a=new Date(a.getTime());a.setDate(a.getDate()+b);return a};f.addHours=function(a,b){return(this||e).addMinutes(a,60*b)};f.addMinutes=function(a,b){return(this||
|
|
15
|
+
e).addSeconds(a,60*b)};f.addSeconds=function(a,b){return(this||e).addMilliseconds(a,1E3*b)};f.addMilliseconds=function(a,b){return new Date(a.getTime()+b)};f.subtract=function(a,b){var c=a.getTime()-b.getTime();return{toMilliseconds:function(){return c},toSeconds:function(){return c/1E3},toMinutes:function(){return c/6E4},toHours:function(){return c/36E5},toDays:function(){return c/864E5}}};f.isLeapYear=function(a){return!(a%4)&&!!(a%100)||!(a%400)};f.isSameDay=function(a,b){return a.toDateString()===
|
|
16
|
+
b.toDateString()};f.locale=function(a,b){p[a]||(p[a]=b)};f.plugin=function(a,b){m[a]||(m[a]=b)};var v=n(f);var e=n(f);e.locale=function(a){a="function"===typeof a?a:e.locale[a];if(!a)return r;r=a(f);var b=p[r]||{},c=n(t,b.res,!0);a=n(w,b.formatter,!0,c);b=n(x,b.parser,!0,c);e._formatter=v._formatter=a;e._parser=v._parser=b;for(var d in m)e.extend(m[d]);return r};e.extend=function(a){var b=n(e._parser.res,a.res),c=a.extender||{};e._formatter=n(e._formatter,a.formatter,!1,b);e._parser=n(e._parser,a.parser,
|
|
17
|
+
!1,b);for(var d in c)e[d]||(e[d]=c[d])};e.plugin=function(a){(a="function"===typeof a?a:e.plugin[a])&&e.extend(m[a(f,v)]||{})};return e})
|
package/esm/date-and-time.es.js
CHANGED
|
@@ -42,6 +42,11 @@ var locales = {},
|
|
|
42
42
|
var offset = d.getTimezoneOffset() / 0.6 | 0;
|
|
43
43
|
return (offset > 0 ? '-' : '+') + ('000' + Math.abs(offset - (offset % 100 * 0.4 | 0))).slice(-4);
|
|
44
44
|
},
|
|
45
|
+
ZZ: function (d/*, formatString*/) {
|
|
46
|
+
var offset = d.getTimezoneOffset();
|
|
47
|
+
var mod = Math.abs(offset);
|
|
48
|
+
return (offset > 0 ? '-' : '+') + ('0' + (mod / 60 | 0)).slice(-2) + ':' + ('0' + mod % 60).slice(-2);
|
|
49
|
+
},
|
|
45
50
|
post: function (str) { return str; },
|
|
46
51
|
res: _res
|
|
47
52
|
},
|
|
@@ -87,6 +92,10 @@ var locales = {},
|
|
|
87
92
|
result.value = (result.value / 100 | 0) * -60 - result.value % 100;
|
|
88
93
|
return result;
|
|
89
94
|
},
|
|
95
|
+
ZZ: function (str/*, formatString */) {
|
|
96
|
+
var arr = /^([\+-])(\d{2}):([0-5]\d)/.exec(str) || ['', '', '', ''];
|
|
97
|
+
return { value: 0 - ((arr[1] + arr[2] | 0) * 60 + (arr[1] + arr[3] | 0)), length: arr[0].length };
|
|
98
|
+
},
|
|
90
99
|
h12: function (h, a) { return (h === 12 ? 0 : h) + a * 12; },
|
|
91
100
|
exec: function (re, str) {
|
|
92
101
|
var result = (re.exec(str) || [''])[0];
|
|
@@ -4,11 +4,11 @@
|
|
|
4
4
|
var e={},f={},m="en",n={MMMM:"January February March April May June July August September October November December".split(" "),MMM:"Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec".split(" "),dddd:"Sunday Monday Tuesday Wednesday Thursday Friday Saturday".split(" "),ddd:"Sun Mon Tue Wed Thu Fri Sat".split(" "),dd:"Su Mo Tu We Th Fr Sa".split(" "),A:["AM","PM"]},q={YYYY:function(a){return("000"+a.getFullYear()).slice(-4)},YY:function(a){return("0"+a.getFullYear()).slice(-2)},Y:function(a){return""+
|
|
5
5
|
a.getFullYear()},MMMM:function(a){return this.res.MMMM[a.getMonth()]},MMM:function(a){return this.res.MMM[a.getMonth()]},MM:function(a){return("0"+(a.getMonth()+1)).slice(-2)},M:function(a){return""+(a.getMonth()+1)},DD:function(a){return("0"+a.getDate()).slice(-2)},D:function(a){return""+a.getDate()},HH:function(a){return("0"+a.getHours()).slice(-2)},H:function(a){return""+a.getHours()},A:function(a){return this.res.A[11<a.getHours()|0]},hh:function(a){return("0"+(a.getHours()%12||12)).slice(-2)},
|
|
6
6
|
h:function(a){return""+(a.getHours()%12||12)},mm:function(a){return("0"+a.getMinutes()).slice(-2)},m:function(a){return""+a.getMinutes()},ss:function(a){return("0"+a.getSeconds()).slice(-2)},s:function(a){return""+a.getSeconds()},SSS:function(a){return("00"+a.getMilliseconds()).slice(-3)},SS:function(a){return("0"+(a.getMilliseconds()/10|0)).slice(-2)},S:function(a){return""+(a.getMilliseconds()/100|0)},dddd:function(a){return this.res.dddd[a.getDay()]},ddd:function(a){return this.res.ddd[a.getDay()]},
|
|
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)},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
|
-
|
|
9
|
-
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},
|
|
10
|
-
res:n};function t(a,b,c,d){var h={},g;for(g in a)h[g]=a[g];for(g in b||{})!!c^!!h[g]||(h[g]=b[g]);d&&(h.res=d);return h}var u={_formatter:q,_parser:r},w,x;
|
|
11
|
-
u.format=function(a,b,c){var d=this||x;b="string"===typeof b?d.compile(b):b;var h=a.getTimezoneOffset();a=d.addMinutes(a,c?h:0);d=d._formatter;var g="";a.getTimezoneOffset=function(){return c?0:h};for(var l=1,v=b.length,k;l<v;l++)k=b[l],g+=d[k]?d.post(d[k](a,b[0])):k.replace(/\[(.*)]/,"$1");return g};
|
|
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
|
+
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
|
+
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 c=-1,d=0,h=0,g=a.length,l;h<g;h++)l=a[h],!b.indexOf(l)&&l.length>d&&(c=h,d=l.length);return{value:c,length:d}},pre:function(a){return a},res:n};function t(a,b,c,d){var h={},g;for(g in a)h[g]=a[g];for(g in b||{})!!c^!!h[g]||(h[g]=b[g]);d&&(h.res=d);return h}var u={_formatter:q,_parser:r},w,x;
|
|
11
|
+
u.compile=function(a){for(var b=/\[([^\[\]]|\[[^\[\]]*])*]|([A-Za-z])\2+|\.{3}|./g,c,d=[a];c=b.exec(a);)d[d.length]=c[0];return d};u.format=function(a,b,c){var d=this||x;b="string"===typeof b?d.compile(b):b;var h=a.getTimezoneOffset();a=d.addMinutes(a,c?h:0);d=d._formatter;var g="";a.getTimezoneOffset=function(){return c?0:h};for(var l=1,v=b.length,k;l<v;l++)k=b[l],g+=d[k]?d.post(d[k](a,b[0])):k.replace(/\[(.*)]/,"$1");return g};
|
|
12
12
|
u.preparse=function(a,b){var c=this||x;b="string"===typeof b?c.compile(b):b;var d={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=/\[(.*)]/;c=c._parser;var g=0;a=c.pre(a);for(var l=1,v=b.length,k,p;l<v;l++)if(k=b[l],c[k]){p=c[k](a.slice(g),b[0]);if(!p.length)break;g+=p.length;d[p.token||k.charAt(0)]=p.value;d._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}d.H=d.H||c.h12(d.h,
|
|
13
13
|
d.A);d._index=g;d._length=a.length;return d};u.parse=function(a,b,c){var d=this||x;a=d.preparse(a,b);return d.isValid(a)?(a.M-=100>a.Y?22801:1,c||a.Z?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
14
|
u.isValid=function(a,b){var c=this||x;a="string"===typeof a?c.preparse(a,b):a;c=[31,28+c.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>c||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,c,d){let h=this||x;return h.format(h.parse(a,b),c,d)};u.addYears=function(a,b){return(this||x).addMonths(a,12*b)};
|
package/esm/date-and-time.mjs
CHANGED
|
@@ -42,6 +42,11 @@ var locales = {},
|
|
|
42
42
|
var offset = d.getTimezoneOffset() / 0.6 | 0;
|
|
43
43
|
return (offset > 0 ? '-' : '+') + ('000' + Math.abs(offset - (offset % 100 * 0.4 | 0))).slice(-4);
|
|
44
44
|
},
|
|
45
|
+
ZZ: function (d/*, formatString*/) {
|
|
46
|
+
var offset = d.getTimezoneOffset();
|
|
47
|
+
var mod = Math.abs(offset);
|
|
48
|
+
return (offset > 0 ? '-' : '+') + ('0' + (mod / 60 | 0)).slice(-2) + ':' + ('0' + mod % 60).slice(-2);
|
|
49
|
+
},
|
|
45
50
|
post: function (str) { return str; },
|
|
46
51
|
res: _res
|
|
47
52
|
},
|
|
@@ -87,6 +92,10 @@ var locales = {},
|
|
|
87
92
|
result.value = (result.value / 100 | 0) * -60 - result.value % 100;
|
|
88
93
|
return result;
|
|
89
94
|
},
|
|
95
|
+
ZZ: function (str/*, formatString */) {
|
|
96
|
+
var arr = /^([\+-])(\d{2}):([0-5]\d)/.exec(str) || ['', '', '', ''];
|
|
97
|
+
return { value: 0 - ((arr[1] + arr[2] | 0) * 60 + (arr[1] + arr[3] | 0)), length: arr[0].length };
|
|
98
|
+
},
|
|
90
99
|
h12: function (h, a) { return (h === 12 ? 0 : h) + a * 12; },
|
|
91
100
|
exec: function (re, str) {
|
|
92
101
|
var result = (re.exec(str) || [''])[0];
|
|
@@ -58,13 +58,17 @@ var plugin = function (date, localized_date) {
|
|
|
58
58
|
}
|
|
59
59
|
return NaN;
|
|
60
60
|
};
|
|
61
|
+
var transformTZ = function (dateString, arg1, arg2, timeZone) {
|
|
62
|
+
return formatTZ(localized_date.parse(dateString, arg1), arg2, timeZone);
|
|
63
|
+
};
|
|
61
64
|
|
|
62
65
|
var name = 'timezone';
|
|
63
66
|
|
|
64
67
|
date.plugin(name, {
|
|
65
68
|
extender: {
|
|
66
69
|
formatTZ: formatTZ,
|
|
67
|
-
parseTZ: parseTZ
|
|
70
|
+
parseTZ: parseTZ,
|
|
71
|
+
transformTZ: transformTZ
|
|
68
72
|
}
|
|
69
73
|
});
|
|
70
74
|
return name;
|
package/esm/plugin/timezone.mjs
CHANGED
|
@@ -58,13 +58,17 @@ var plugin = function (date, localized_date) {
|
|
|
58
58
|
}
|
|
59
59
|
return NaN;
|
|
60
60
|
};
|
|
61
|
+
var transformTZ = function (dateString, arg1, arg2, timeZone) {
|
|
62
|
+
return formatTZ(localized_date.parse(dateString, arg1), arg2, timeZone);
|
|
63
|
+
};
|
|
61
64
|
|
|
62
65
|
var name = 'timezone';
|
|
63
66
|
|
|
64
67
|
date.plugin(name, {
|
|
65
68
|
extender: {
|
|
66
69
|
formatTZ: formatTZ,
|
|
67
|
-
parseTZ: parseTZ
|
|
70
|
+
parseTZ: parseTZ,
|
|
71
|
+
transformTZ: transformTZ
|
|
68
72
|
}
|
|
69
73
|
});
|
|
70
74
|
return name;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "date-and-time",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.2.0",
|
|
4
4
|
"description": "A Minimalist DateTime utility for Node.js and the browser",
|
|
5
5
|
"main": "date-and-time.js",
|
|
6
6
|
"module": "esm/date-and-time.es.js",
|
|
@@ -46,8 +46,8 @@
|
|
|
46
46
|
"devDependencies": {
|
|
47
47
|
"@ampproject/rollup-plugin-closure-compiler": "^0.27.0",
|
|
48
48
|
"expect.js": "^0.3.1",
|
|
49
|
-
"mocha": "^9.2.
|
|
49
|
+
"mocha": "^9.2.1",
|
|
50
50
|
"mocha-headless-chrome": "^4.0.0",
|
|
51
|
-
"rollup": "^2.
|
|
51
|
+
"rollup": "^2.68.0"
|
|
52
52
|
}
|
|
53
53
|
}
|
package/plugin/timezone.js
CHANGED
|
@@ -64,13 +64,17 @@
|
|
|
64
64
|
}
|
|
65
65
|
return NaN;
|
|
66
66
|
};
|
|
67
|
+
var transformTZ = function (dateString, arg1, arg2, timeZone) {
|
|
68
|
+
return formatTZ(localized_date.parse(dateString, arg1), arg2, timeZone);
|
|
69
|
+
};
|
|
67
70
|
|
|
68
71
|
var name = 'timezone';
|
|
69
72
|
|
|
70
73
|
date.plugin(name, {
|
|
71
74
|
extender: {
|
|
72
75
|
formatTZ: formatTZ,
|
|
73
|
-
parseTZ: parseTZ
|
|
76
|
+
parseTZ: parseTZ,
|
|
77
|
+
transformTZ: transformTZ
|
|
74
78
|
}
|
|
75
79
|
});
|
|
76
80
|
return name;
|