date-and-time 0.14.2 → 2.0.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/EXTEND.md +29 -11
- package/LOCALE.md +19 -34
- package/PLUGINS.md +222 -113
- package/README.md +203 -171
- package/date-and-time.js +171 -147
- package/date-and-time.min.js +14 -14
- package/esm/date-and-time.es.js +466 -0
- package/esm/date-and-time.es.min.js +17 -0
- package/esm/date-and-time.mjs +466 -0
- package/esm/locale/ar.es.js +39 -0
- package/esm/locale/ar.mjs +39 -0
- package/esm/locale/az.es.js +44 -0
- package/esm/locale/az.mjs +44 -0
- package/esm/locale/bn.es.js +50 -0
- package/esm/locale/bn.mjs +50 -0
- package/esm/locale/cs.es.js +22 -0
- package/esm/locale/cs.mjs +22 -0
- package/esm/locale/de.es.js +23 -0
- package/esm/locale/de.mjs +23 -0
- package/esm/locale/dk.es.js +22 -0
- package/esm/locale/dk.mjs +22 -0
- package/esm/locale/el.es.js +44 -0
- package/esm/locale/el.mjs +44 -0
- package/esm/locale/en.es.js +13 -0
- package/esm/locale/en.mjs +13 -0
- package/esm/locale/es.es.js +42 -0
- package/esm/locale/es.mjs +42 -0
- package/esm/locale/fa.es.js +39 -0
- package/esm/locale/fa.mjs +39 -0
- package/esm/locale/fr.es.js +23 -0
- package/esm/locale/fr.mjs +23 -0
- package/esm/locale/hi.es.js +50 -0
- package/esm/locale/hi.mjs +50 -0
- package/esm/locale/hu.es.js +23 -0
- package/esm/locale/hu.mjs +23 -0
- package/esm/locale/id.es.js +46 -0
- package/esm/locale/id.mjs +46 -0
- package/esm/locale/it.es.js +23 -0
- package/esm/locale/it.mjs +23 -0
- package/esm/locale/ja.es.js +31 -0
- package/esm/locale/ja.mjs +31 -0
- package/esm/locale/jv.es.js +46 -0
- package/esm/locale/jv.mjs +46 -0
- package/esm/locale/ko.es.js +23 -0
- package/esm/locale/ko.mjs +23 -0
- package/esm/locale/my.es.js +38 -0
- package/esm/locale/my.mjs +38 -0
- package/esm/locale/nl.es.js +37 -0
- package/esm/locale/nl.mjs +37 -0
- package/esm/locale/pa-in.es.js +62 -0
- package/esm/locale/pa-in.mjs +62 -0
- package/esm/locale/pl.es.js +37 -0
- package/esm/locale/pl.mjs +37 -0
- package/esm/locale/pt.es.js +44 -0
- package/esm/locale/pt.mjs +44 -0
- package/esm/locale/ro.es.js +22 -0
- package/esm/locale/ro.mjs +22 -0
- package/esm/locale/ru.es.js +44 -0
- package/esm/locale/ru.mjs +44 -0
- package/esm/locale/rw.es.js +22 -0
- package/esm/locale/rw.mjs +22 -0
- package/esm/locale/sr.es.js +22 -0
- package/esm/locale/sr.mjs +22 -0
- package/esm/locale/th.es.js +23 -0
- package/esm/locale/th.mjs +23 -0
- package/esm/locale/tr.es.js +22 -0
- package/esm/locale/tr.mjs +22 -0
- package/esm/locale/uk.es.js +57 -0
- package/esm/locale/uk.mjs +57 -0
- package/esm/locale/uz.es.js +22 -0
- package/esm/locale/uz.mjs +22 -0
- package/esm/locale/vi.es.js +23 -0
- package/esm/locale/vi.mjs +23 -0
- package/esm/locale/zh-cn.es.js +48 -0
- package/esm/locale/zh-cn.mjs +48 -0
- package/esm/locale/zh-tw.es.js +46 -0
- package/esm/locale/zh-tw.mjs +46 -0
- package/esm/plugin/day-of-week.es.js +19 -0
- package/esm/plugin/day-of-week.mjs +19 -0
- package/esm/plugin/meridiem.es.js +47 -0
- package/esm/plugin/meridiem.mjs +47 -0
- package/esm/plugin/microsecond.es.js +31 -0
- package/esm/plugin/microsecond.mjs +31 -0
- package/esm/plugin/ordinal.es.js +34 -0
- package/esm/plugin/ordinal.mjs +34 -0
- package/esm/plugin/timespan.es.js +75 -0
- package/esm/plugin/timespan.mjs +75 -0
- package/esm/plugin/timezone.es.js +73 -0
- package/esm/plugin/timezone.mjs +73 -0
- package/esm/plugin/two-digit-year.es.js +21 -0
- package/esm/plugin/two-digit-year.mjs +21 -0
- package/locale/ar.js +14 -19
- package/locale/az.js +14 -19
- package/locale/bn.js +14 -19
- package/locale/cs.js +14 -19
- package/locale/de.js +15 -20
- package/locale/dk.js +15 -20
- package/locale/el.js +21 -24
- package/locale/en.js +17 -19
- package/locale/es.js +14 -19
- package/locale/fa.js +14 -19
- package/locale/fr.js +14 -19
- package/locale/hi.js +14 -19
- package/locale/hu.js +14 -19
- package/locale/id.js +14 -19
- package/locale/it.js +14 -19
- package/locale/ja.js +14 -19
- package/locale/jv.js +14 -19
- package/locale/ko.js +14 -19
- package/locale/my.js +14 -19
- package/locale/nl.js +20 -23
- package/locale/pa-in.js +14 -19
- package/locale/pl.js +20 -23
- package/locale/pt.js +14 -19
- package/locale/ro.js +15 -20
- package/locale/ru.js +14 -19
- package/locale/rw.js +30 -0
- package/locale/sr.js +14 -19
- package/locale/th.js +14 -19
- package/locale/tr.js +14 -19
- package/locale/uk.js +23 -26
- package/locale/uz.js +14 -19
- package/locale/vi.js +14 -19
- package/locale/zh-cn.js +14 -19
- package/locale/zh-tw.js +14 -19
- package/package.json +26 -11
- package/plugin/day-of-week.js +13 -14
- package/plugin/meridiem.js +32 -24
- package/plugin/microsecond.js +14 -15
- package/plugin/ordinal.js +13 -14
- package/plugin/timespan.js +13 -14
- package/plugin/timezone.js +81 -0
- package/plugin/two-digit-year.js +13 -19
package/PLUGINS.md
CHANGED
|
@@ -1,112 +1,110 @@
|
|
|
1
1
|
# Plugins
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
This library is oriented towards minimalism, so it may seem to some developers to be lacking in features. Plugin is the most realistic solution to such dissatisfaction. By importing plugins, you can extend the functionality of this library, mainly a formatter and a parser.
|
|
4
|
+
|
|
5
|
+
*The formatter is used in `format()`, etc., the parser is used in `parse()`, `preparse()`, `isValid()`, etc.*
|
|
4
6
|
|
|
5
7
|
## Usage
|
|
6
8
|
|
|
7
|
-
|
|
9
|
+
- ES Modules:
|
|
10
|
+
|
|
11
|
+
```javascript
|
|
12
|
+
import date from 'date-and-time';
|
|
13
|
+
// Import the plugin named "foobar".
|
|
14
|
+
import foobar from 'date-and-time/plugin/foobar';
|
|
15
|
+
|
|
16
|
+
// Apply the "foobar" to the library.
|
|
17
|
+
date.plugin(foobar);
|
|
18
|
+
```
|
|
8
19
|
|
|
9
20
|
- CommonJS:
|
|
10
21
|
|
|
11
22
|
```javascript
|
|
12
23
|
const date = require('date-and-time');
|
|
13
|
-
// Import the plugin "foobar".
|
|
24
|
+
// Import the plugin named "foobar".
|
|
14
25
|
const foobar = require('date-and-time/plugin/foobar');
|
|
15
26
|
|
|
16
|
-
// Apply the
|
|
27
|
+
// Apply the "foobar" to the library.
|
|
17
28
|
date.plugin(foobar);
|
|
18
29
|
```
|
|
19
30
|
|
|
20
|
-
- ES Modules
|
|
31
|
+
- ES Modules for the browser:
|
|
21
32
|
|
|
22
|
-
```
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
33
|
+
```html
|
|
34
|
+
<script type="module">
|
|
35
|
+
import date from '/path/to/date-and-time.es.min.js';
|
|
36
|
+
// Import the plugin named "foobar".
|
|
37
|
+
import foobar from '/path/to/date-and-time/plugin/foobar.es.js';
|
|
26
38
|
|
|
27
|
-
// Apply the
|
|
39
|
+
// Apply the "foobar" to the library.
|
|
28
40
|
date.plugin(foobar);
|
|
41
|
+
</script>
|
|
29
42
|
```
|
|
30
43
|
|
|
31
44
|
- Older browser:
|
|
32
45
|
|
|
33
|
-
When in older browser, pass the plugin name as before. (no changes)
|
|
34
|
-
|
|
35
46
|
```html
|
|
36
47
|
<script src="/path/to/date-and-time.min.js"></script>
|
|
37
|
-
<!-- Import the plugin "foobar". -->
|
|
48
|
+
<!-- Import the plugin named "foobar". -->
|
|
38
49
|
<script src="/path/to/plugin/foobar.js"></script>
|
|
39
50
|
|
|
40
51
|
<script>
|
|
41
|
-
// Apply the
|
|
52
|
+
// Apply the "foobar" to the library.
|
|
42
53
|
date.plugin('foobar');
|
|
43
54
|
</script>
|
|
44
55
|
```
|
|
45
56
|
|
|
46
|
-
###
|
|
47
|
-
|
|
48
|
-
The following (old) methods are deprecated. In the next version it won't be able to use them.
|
|
49
|
-
|
|
50
|
-
- CommonJS:
|
|
51
|
-
|
|
52
|
-
```javascript
|
|
53
|
-
const date = require('date-and-time');
|
|
54
|
-
// Import the plugin "foobar".
|
|
55
|
-
require('date-and-time/plugin/foobar');
|
|
56
|
-
|
|
57
|
-
// Apply the plugin to "date-and-time".
|
|
58
|
-
date.plugin('foobar');
|
|
59
|
-
```
|
|
60
|
-
|
|
61
|
-
- ES Modules (with transpile):
|
|
57
|
+
### Note
|
|
62
58
|
|
|
63
|
-
|
|
64
|
-
import date from 'date-and-time';
|
|
65
|
-
// Import the plugin "foobar".
|
|
66
|
-
import 'date-and-time/plugin/foobar';
|
|
67
|
-
|
|
68
|
-
// Apply the plugin to "date-and-time".
|
|
69
|
-
date.plugin('foobar');
|
|
70
|
-
```
|
|
59
|
+
- If you want to use ES Modules in Node.js without a transpiler, you need to add `"type": "module"` in your `package.json` or change your file extension from `.js` to `.mjs`.
|
|
71
60
|
|
|
72
61
|
## Plugin List
|
|
73
62
|
|
|
74
|
-
- day-of-week
|
|
75
|
-
- It adds day of week
|
|
63
|
+
- [day-of-week](#day-of-week)
|
|
64
|
+
- It adds *"dummy"* tokens for `day of week` to the parser.
|
|
76
65
|
|
|
77
|
-
- meridiem
|
|
78
|
-
- It
|
|
66
|
+
- [meridiem](#meridiem)
|
|
67
|
+
- It adds various notations for `AM PM`.
|
|
79
68
|
|
|
80
|
-
- microsecond
|
|
81
|
-
- It adds microsecond
|
|
69
|
+
- [microsecond](#microsecond)
|
|
70
|
+
- It adds tokens for microsecond to the parser.
|
|
82
71
|
|
|
83
|
-
- ordinal
|
|
72
|
+
- [ordinal](#ordinal)
|
|
84
73
|
- It adds ordinal notation of date to the formatter.
|
|
85
74
|
|
|
86
|
-
- timespan
|
|
87
|
-
- It adds `timeSpan()` function to the library.
|
|
75
|
+
- [timespan](#timespan)
|
|
76
|
+
- It adds `timeSpan()` function that calculates the difference of two dates to the library.
|
|
77
|
+
|
|
78
|
+
- [timezone](#timezone)
|
|
79
|
+
- It adds `formatTZ()` and `parseTZ()` functions that support `IANA time zone names` to the library.
|
|
88
80
|
|
|
89
|
-
- two-digit-year
|
|
81
|
+
- [two-digit-year](#two-digit-year)
|
|
90
82
|
- It adds two-digit year notation to the parser.
|
|
91
83
|
|
|
92
|
-
|
|
84
|
+
---
|
|
93
85
|
|
|
94
86
|
### day-of-week
|
|
95
87
|
|
|
96
|
-
It adds `
|
|
88
|
+
It adds tokens for `day of week` to the parser. Although `day of week` is not significant information for the parser to identify a date, these tokens are sometimes useful. For example, when a string to be parsed contains a day of week, and you just want to skip it.
|
|
89
|
+
|
|
90
|
+
**formatter:**
|
|
91
|
+
|
|
92
|
+
There is no change.
|
|
93
|
+
|
|
94
|
+
**parser:**
|
|
97
95
|
|
|
98
|
-
| token | meaning
|
|
99
|
-
|
|
100
|
-
| dddd |
|
|
101
|
-
| ddd |
|
|
102
|
-
| dd |
|
|
96
|
+
| token | meaning | acceptable examples |
|
|
97
|
+
|:------|:-----------|:--------------------|
|
|
98
|
+
| dddd | long | Friday, Sunday |
|
|
99
|
+
| ddd | short | Fri, Sun |
|
|
100
|
+
| dd | very short | Fr, Su |
|
|
103
101
|
|
|
104
102
|
```javascript
|
|
105
103
|
const date = require('date-and-time');
|
|
106
|
-
// Import "day-of-week" plugin.
|
|
104
|
+
// Import "day-of-week" plugin as a named "day_of_week".
|
|
107
105
|
const day_of_week = require('date-and-time/plugin/day-of-week');
|
|
108
106
|
|
|
109
|
-
// Apply "
|
|
107
|
+
// Apply the "day_of_week" plugin to the library.
|
|
110
108
|
date.plugin(day_of_week);
|
|
111
109
|
|
|
112
110
|
// You can write like this.
|
|
@@ -116,29 +114,34 @@ date.parse('Thursday, March 05, 2020', ' , MMMM, D YYYY');
|
|
|
116
114
|
date.parse('Friday, March 06, 2020', ' , MMMM, D YYYY');
|
|
117
115
|
```
|
|
118
116
|
|
|
117
|
+
---
|
|
118
|
+
|
|
119
119
|
### meridiem
|
|
120
120
|
|
|
121
|
-
It adds
|
|
121
|
+
It adds various notations for AM PM.
|
|
122
|
+
|
|
123
|
+
**formatter:**
|
|
122
124
|
|
|
123
|
-
| token | meaning
|
|
124
|
-
|
|
125
|
-
|
|
|
126
|
-
|
|
|
127
|
-
|
|
|
128
|
-
| aa | meridiem (lowercase with ellipsis) | a.m., p.m. | ✔ |
|
|
125
|
+
| token | meaning | output examples |
|
|
126
|
+
|:------|:------------------------|:----------------|
|
|
127
|
+
| AA | uppercase with ellipsis | A.M., P.M. |
|
|
128
|
+
| a | lowercase | am, pm |
|
|
129
|
+
| aa | lowercase with ellipsis | a.m., p.m. |
|
|
129
130
|
|
|
130
|
-
|
|
131
|
+
**parser:**
|
|
131
132
|
|
|
132
|
-
| token | meaning
|
|
133
|
-
|
|
134
|
-
|
|
|
133
|
+
| token | meaning | acceptable examples |
|
|
134
|
+
|:------|:------------------------|:--------------------|
|
|
135
|
+
| AA | uppercase with ellipsis | A.M., P.M. |
|
|
136
|
+
| a | lowercase | am, pm |
|
|
137
|
+
| aa | lowercase with ellipsis | a.m., p.m. |
|
|
135
138
|
|
|
136
139
|
```javascript
|
|
137
140
|
const date = require('date-and-time');
|
|
138
141
|
// Import "meridiem" plugin.
|
|
139
142
|
const meridiem = require('date-and-time/plugin/meridiem');
|
|
140
143
|
|
|
141
|
-
// Apply "medidiem" plugin to
|
|
144
|
+
// Apply "medidiem" plugin to the library.
|
|
142
145
|
date.plugin(meridiem);
|
|
143
146
|
|
|
144
147
|
// This is default behavior of the formatter.
|
|
@@ -149,35 +152,44 @@ date.format(new Date(), 'hh:mm AA'); // => '12:34 P.M.'
|
|
|
149
152
|
date.format(new Date(), 'hh:mm a'); // => '12:34 pm'
|
|
150
153
|
date.format(new Date(), 'hh:mm aa'); // => '12:34 p.m.'
|
|
151
154
|
|
|
152
|
-
//
|
|
155
|
+
// This is default behavior of the parser.
|
|
153
156
|
date.parse('12:34 PM', 'hh:mm A'); // => Jan 1 1970 12:34:00
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
date.parse('12:34
|
|
157
|
+
|
|
158
|
+
// These are added tokens to the parser.
|
|
159
|
+
date.parse('12:34 P.M.', 'hh:mm AA'); // => Jan 1 1970 12:34:00
|
|
160
|
+
date.parse('12:34 pm', 'hh:mm a'); // => Jan 1 1970 12:34:00
|
|
161
|
+
date.parse('12:34 p.m.', 'hh:mm aa'); // => Jan 1 1970 12:34:00
|
|
157
162
|
```
|
|
158
163
|
|
|
164
|
+
This plugin has a **breaking change**. In previous versions, the `A` token for the parser could parse various notations for AM PM, but in the new version, it can only parse `AM` and `PM`. For other notations, a dedicated token is now provided for each.
|
|
165
|
+
|
|
166
|
+
---
|
|
167
|
+
|
|
159
168
|
### microsecond
|
|
160
169
|
|
|
161
|
-
It adds
|
|
170
|
+
It adds tokens for microsecond to the parser. If a time string to be parsed contains microsecond, these tokens are useful. In JS, however, it is not supported microsecond accuracy, a parsed value is rounded to millisecond accuracy.
|
|
171
|
+
|
|
172
|
+
**formatter:**
|
|
162
173
|
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
|
168
|
-
|
|
169
|
-
|
|
|
170
|
-
|
|
|
174
|
+
There is no change.
|
|
175
|
+
|
|
176
|
+
**parser:**
|
|
177
|
+
|
|
178
|
+
| token | meaning | acceptable examples |
|
|
179
|
+
|:-------|:----------------|:--------------------|
|
|
180
|
+
| SSSSSS | high accuracy | 753123, 022113 |
|
|
181
|
+
| SSSSS | middle accuracy | 75312, 02211 |
|
|
182
|
+
| SSSS | low accuracy | 7531, 0221 |
|
|
171
183
|
|
|
172
184
|
```javascript
|
|
173
185
|
const date = require('date-and-time');
|
|
174
186
|
// Import "microsecond" plugin.
|
|
175
187
|
const microsecond = require('date-and-time/plugin/microsecond');
|
|
176
188
|
|
|
177
|
-
// Apply "microsecond" plugin to
|
|
189
|
+
// Apply "microsecond" plugin to the library.
|
|
178
190
|
date.plugin(microsecond);
|
|
179
191
|
|
|
180
|
-
// A date object in JavaScript supports `millisecond` (ms):
|
|
192
|
+
// A date object in JavaScript supports `millisecond` (ms) like this:
|
|
181
193
|
date.parse('12:34:56.123', 'HH:mm:ss.SSS');
|
|
182
194
|
|
|
183
195
|
// 4 or more digits number sometimes seen is not `millisecond`, probably `microsecond` (μs):
|
|
@@ -186,22 +198,28 @@ date.parse('12:34:56.123456', 'HH:mm:ss.SSSSSS');
|
|
|
186
198
|
// 123456µs will be rounded to 123ms.
|
|
187
199
|
```
|
|
188
200
|
|
|
201
|
+
---
|
|
202
|
+
|
|
189
203
|
### ordinal
|
|
190
204
|
|
|
191
|
-
It adds `DDD` token
|
|
205
|
+
It adds `DDD` token that output ordinal notation of date to the formatter.
|
|
206
|
+
|
|
207
|
+
**formatter:**
|
|
208
|
+
|
|
209
|
+
| token | meaning | output examples |
|
|
210
|
+
|:------|:-------------------------|:--------------------|
|
|
211
|
+
| DDD | ordinal notation of date | 1st, 2nd, 3rd, 31th |
|
|
192
212
|
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
| DD | date with zero-padding | 01, 02, 03, 31 | |
|
|
197
|
-
| D | date | 1, 2, 3, 31 | |
|
|
213
|
+
**parser:**
|
|
214
|
+
|
|
215
|
+
There is no change.
|
|
198
216
|
|
|
199
217
|
```javascript
|
|
200
218
|
const date = require('date-and-time');
|
|
201
219
|
// Import "ordinal" plugin.
|
|
202
220
|
const ordinal = require('date-and-time/plugin/ordinal');
|
|
203
221
|
|
|
204
|
-
// Apply "ordinal" plugin to
|
|
222
|
+
// Apply "ordinal" plugin to the library.
|
|
205
223
|
date.plugin(ordinal);
|
|
206
224
|
|
|
207
225
|
// These are default behavior of the formatter.
|
|
@@ -212,16 +230,24 @@ date.format(new Date(), 'MMM DD YYYY'); // => Jan 01 2019
|
|
|
212
230
|
date.format(new Date(), 'MMM DDD YYYY'); // => Jan 1st 2019
|
|
213
231
|
```
|
|
214
232
|
|
|
233
|
+
---
|
|
234
|
+
|
|
215
235
|
### timespan
|
|
216
236
|
|
|
217
|
-
It adds `timeSpan()` function to the library. This function is similar to
|
|
237
|
+
It adds `timeSpan()` function that calculates the difference of two dates to the library. This function is similar to `subtract()`, the difference is that it can format the calculation results.
|
|
238
|
+
|
|
239
|
+
#### timeSpan(date1, date2)
|
|
240
|
+
|
|
241
|
+
- @param {**Date**} date1 - a Date object
|
|
242
|
+
- @param {**Date**} date2 - a Date object
|
|
243
|
+
- @returns {**Object**} a result object subtracting date2 from date1
|
|
218
244
|
|
|
219
245
|
```javascript
|
|
220
246
|
const date = require('date-and-time');
|
|
221
247
|
// Import "timespan" plugin.
|
|
222
248
|
const timespan = require('date-and-time/plugin/timespan');
|
|
223
249
|
|
|
224
|
-
// Apply "timespan" plugin to
|
|
250
|
+
// Apply "timespan" plugin to the library.
|
|
225
251
|
date.plugin(timespan);
|
|
226
252
|
|
|
227
253
|
const now = new Date(2020, 2, 5, 1, 2, 3, 4);
|
|
@@ -232,7 +258,7 @@ date.timeSpan(now, new_years_day).toHours('H [hours] m [minutes] s [seconds]');
|
|
|
232
258
|
date.timeSpan(now, new_years_day).toMinutes('mmmmmmmmmm [minutes]'); // => '0000092222 minutes'
|
|
233
259
|
```
|
|
234
260
|
|
|
235
|
-
|
|
261
|
+
Like `subtract()`, `timeSpan()` returns an object with functions like this:
|
|
236
262
|
|
|
237
263
|
| function | description |
|
|
238
264
|
|:---------------|:------------------------|
|
|
@@ -242,7 +268,7 @@ The `timeSpan()` returns an object that has some functions as with the `subtract
|
|
|
242
268
|
| toSeconds | Outputs as seconds |
|
|
243
269
|
| toMilliseconds | Outputs as milliseconds |
|
|
244
270
|
|
|
245
|
-
|
|
271
|
+
In these functions can be available some tokens to format the calculation result. Here are the tokens and their meanings:
|
|
246
272
|
|
|
247
273
|
| function | available tokens |
|
|
248
274
|
|:---------------|:-----------------|
|
|
@@ -260,36 +286,119 @@ Available tokens in those functions and their meanings are as follows:
|
|
|
260
286
|
| s | second |
|
|
261
287
|
| S | millisecond |
|
|
262
288
|
|
|
289
|
+
---
|
|
290
|
+
|
|
291
|
+
### timezone
|
|
292
|
+
|
|
293
|
+
It adds `formatTZ()` and `parseTZ()` functions that support `IANA time zone names` (`America/Los_Angeles`, `Asia/Tokyo`, and so on) to the library.
|
|
294
|
+
|
|
295
|
+
#### formatTZ(dateObj, arg[, timeZone])
|
|
296
|
+
|
|
297
|
+
- @param {**Date**} dateObj - a Date object
|
|
298
|
+
- @param {**string|Array.\<string\>**} arg - a format string or its compiled object
|
|
299
|
+
- @param {**string**} [timeZone] - output as this time zone
|
|
300
|
+
- @returns {**string**} a formatted string
|
|
301
|
+
|
|
302
|
+
The `formatTZ()` is upward compatible with `format()`. Tokens available here are the same as for the `format()`. If the `timeZone` is omitted, it output the date string with local time zone.
|
|
303
|
+
|
|
304
|
+
#### parseTZ(dateString, arg[, timeZone])
|
|
305
|
+
|
|
306
|
+
- @param {**string**} dateString - a date string
|
|
307
|
+
- @param {**string|Array.\<string\>**} arg - a format string or its compiled object
|
|
308
|
+
- @param {**string**} [timeZone] - input as this time zone
|
|
309
|
+
- @returns {**Date**} a constructed date
|
|
310
|
+
|
|
311
|
+
The `parseTZ()` is upward compatible with `parse()`. Tokens available here are the same as for the `parse()`. If the `timeZone` is omitted, the time zone of the date string is assumed to be local time zone.
|
|
312
|
+
|
|
313
|
+
```javascript
|
|
314
|
+
const date = require('date-and-time');
|
|
315
|
+
// Import "timezone" plugin.
|
|
316
|
+
const timezone = require('date-and-time/plugin/timezone');
|
|
317
|
+
|
|
318
|
+
// Apply "timezone" plugin to the library.
|
|
319
|
+
date.plugin(timezone);
|
|
320
|
+
|
|
321
|
+
const d1 = new Date(Date.UTC(2021, 2, 14, 9, 59, 59, 999)); // 2021-03-14T09:59:59.999Z
|
|
322
|
+
date.formatTZ(d1, 'MMMM DD YYYY H:mm:ss.SSS [UTC]Z', 'America/Los_Angeles'); // March 14 2021 1:59:59.999 UTC-0800
|
|
323
|
+
|
|
324
|
+
const d2 = new Date(Date.UTC(2021, 2, 14, 10, 0, 0, 0)); // 2021-03-14T10:00:00.000Z
|
|
325
|
+
date.formatTZ(d2, 'MMMM DD YYYY H:mm:ss.SSS [UTC]Z', 'America/Los_Angeles'); // March 14 2021 3:00:00.000 UTC-0700
|
|
326
|
+
|
|
327
|
+
// Parses the date string assuming that the time zone is "Pacific/Honolulu" (UTC-1000).
|
|
328
|
+
date.parseTZ('Sep 25 2021 4:00:00', 'MMM D YYYY H:mm:ss', 'Pacific/Honolulu'); // 2021-09-25T14:00:00.000Z
|
|
329
|
+
|
|
330
|
+
// Parses the date string assuming that the time zone is "Europe/London" (UTC+0100).
|
|
331
|
+
date.parseTZ('Sep 25 2021 4:00:00', 'MMM D YYYY H:mm:ss', 'Europe/London'); // 2021-09-25T03:00:00.000Z
|
|
332
|
+
```
|
|
333
|
+
|
|
334
|
+
#### Caveats
|
|
335
|
+
|
|
336
|
+
- 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. The `format()` and the `parse()` are enough.
|
|
338
|
+
|
|
339
|
+
#### Start of DST (Daylight Saving Time)
|
|
340
|
+
|
|
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, there is no `02:00:00` to `02:59:59` on 14 March 2021. In such edge cases, the `parseTZ()` will parse like this:
|
|
342
|
+
|
|
343
|
+
```javascript
|
|
344
|
+
date.parseTZ('Mar 14 2021 1:59:59', 'MMM D YYYY H:mm:ss', 'America/Los_Angeles'); // => 2021-03-14T09:59:59Z
|
|
345
|
+
date.parseTZ('Mar 14 2021 2:00:00', 'MMM D YYYY H:mm:ss', 'America/Los_Angeles'); // => NaN
|
|
346
|
+
date.parseTZ('Mar 14 2021 2:59:59', 'MMM D YYYY H:mm:ss', 'America/Los_Angeles'); // => NaN
|
|
347
|
+
date.parseTZ('Mar 14 2021 3:00:00', 'MMM D YYYY H:mm:ss', 'America/Los_Angeles'); // => 2021-03-14T10:00:00Z
|
|
348
|
+
```
|
|
349
|
+
|
|
350
|
+
#### End of DST
|
|
351
|
+
|
|
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, `01:00:00` to `01:59:59` on November 7 2021 is repeated twice. Since there are two possible times between them, DST or not, the `parseTZ()` assumes that the time is former to make the result unique:
|
|
353
|
+
|
|
354
|
+
```javascript
|
|
355
|
+
// The parseTZ() assumes that this time is DST.
|
|
356
|
+
date.parseTZ('Nov 7 2021 1:59:59', 'MMM D YYYY H:mm:ss', 'America/Los_Angeles'); // => 2021-11-07T08:59:59Z
|
|
357
|
+
// This time is already PST.
|
|
358
|
+
date.parseTZ('Nov 7 2021 2:00:00', 'MMM D YYYY H:mm:ss', 'America/Los_Angeles'); // => 2021-11-07T10:00:00Z
|
|
359
|
+
```
|
|
360
|
+
|
|
361
|
+
At the first example above, if you want the parser to parse the time as PST (Pacific Standard Time), use the `parse()` with a time offset instead:
|
|
362
|
+
|
|
363
|
+
```javascript
|
|
364
|
+
date.parse('Nov 7 2021 1:59:59 -0800', 'MMM D YYYY H:mm:ss Z'); // => 2021-11-07T09:59:59Z
|
|
365
|
+
```
|
|
366
|
+
|
|
367
|
+
---
|
|
368
|
+
|
|
263
369
|
### two-digit-year
|
|
264
370
|
|
|
265
|
-
It adds `YY` token to the parser
|
|
371
|
+
It adds `YY` token to the parser. This token will convert the year 69 or earlier to 2000s, the year 70 or later to 1900s. In brief:
|
|
266
372
|
|
|
267
|
-
|
|
|
268
|
-
|
|
269
|
-
|
|
|
270
|
-
|
|
|
271
|
-
| Y | two-digit year without zero-padding | 90, 0, 8, 19 | ✔ |
|
|
373
|
+
| examples | result |
|
|
374
|
+
|:------------------------|:-------|
|
|
375
|
+
| 00, 01, 02, ..., 68, 69 | 2000s |
|
|
376
|
+
| 70, 71, 72, ..., 98, 99 | 1900s |
|
|
272
377
|
|
|
273
|
-
|
|
378
|
+
**formatter:**
|
|
379
|
+
|
|
380
|
+
There is no change.
|
|
381
|
+
|
|
382
|
+
**parser:**
|
|
383
|
+
|
|
384
|
+
| token | meaning | acceptable examples |
|
|
385
|
+
|:------|:---------------|:--------------------|
|
|
386
|
+
| YY | two-digit year | 90, 00, 08, 19 |
|
|
274
387
|
|
|
275
388
|
```javascript
|
|
276
389
|
const date = require('date-and-time');
|
|
277
|
-
// Import "two-digit-year" plugin.
|
|
390
|
+
// Import "two-digit-year" plugin as a named "two_digit_year".
|
|
278
391
|
const two_digit_year = require('date-and-time/plugin/two-digit-year');
|
|
279
392
|
|
|
280
|
-
//
|
|
393
|
+
// This is the default behavior of the parser.
|
|
281
394
|
date.parse('Dec 25 69', 'MMM D YY'); // => Invalid Date
|
|
282
|
-
date.parse('Dec 25 70', 'MMM D Y'); // => 70 AD (ancient times)
|
|
283
395
|
|
|
284
|
-
// Apply "
|
|
396
|
+
// Apply the "two_digit_year" plugin to the library.
|
|
285
397
|
date.plugin(two_digit_year);
|
|
286
398
|
|
|
287
|
-
//
|
|
399
|
+
// The `YY` token convert the year 69 or earlier to 2000s, the year 70 or later to 1900s.
|
|
288
400
|
date.parse('Dec 25 69', 'MMM D YY'); // => Dec 25 2069
|
|
289
|
-
date.parse('Dec 25 70', 'MMM D
|
|
290
|
-
|
|
291
|
-
// `Y` token will no longer acceptable the year 100 or later.
|
|
292
|
-
date.parse('Dec 25 2019', 'MMM D Y'); // => Invalid Date
|
|
293
|
-
// Use `YYYY` token instead if necessary.
|
|
294
|
-
date.parse('Dec 25 2019', 'MMM D YYYY'); // => Dec 25 2019
|
|
401
|
+
date.parse('Dec 25 70', 'MMM D YY'); // => Dec 25 1970
|
|
295
402
|
```
|
|
403
|
+
|
|
404
|
+
This plugin has a **breaking change**. In previous versions, this plugin overrode the default behavior of the `Y` token, but this has been obsolete.
|