date-and-time 0.12.0 → 0.14.2

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/LOCALE.md CHANGED
@@ -1,39 +1,41 @@
1
1
  # Locale
2
2
 
3
- Month, day of week, and meridiem (am / pm) the `format()` outputs are usually in English, and the `parse()` also assumes that a passed date string is English.
3
+ The `format()` outputs month, day of week, and meridiem (am / pm) in English, and the `parse()` assumes the passed date string is in English. Here it describes how to use other languages in these functions.
4
4
 
5
5
  ## Usage
6
6
 
7
- If you would like to use any other language in these functions, switch as follows:
7
+ To support `ES Modules` in the next version, the locale switching method has changed and then the old method has been deprecated.
8
8
 
9
- - Node.js:
9
+ - CommonJS:
10
10
 
11
11
  ```javascript
12
12
  const date = require('date-and-time');
13
- require('date-and-time/locale/fr');
13
+ const fr = require('date-and-time/locale/fr');
14
14
 
15
- date.locale('fr'); // French
15
+ date.locale(fr); // French
16
16
  date.format(new Date(), 'dddd D MMMM'); // => 'lundi 11 janvier'
17
17
  ```
18
18
 
19
- - With a transpiler:
19
+ - ES Modules (with transpile):
20
20
 
21
21
  ```javascript
22
22
  import date from 'date-and-time';
23
- import 'date-and-time/locale/it';
23
+ import it from 'date-and-time/locale/it';
24
24
 
25
- date.locale('it'); // Italian
25
+ date.locale(it); // Italian
26
26
  date.format(new Date(), 'dddd D MMMM'); // => 'Lunedì 11 gennaio'
27
27
  ```
28
28
 
29
- - With an older browser:
29
+ - Older browser:
30
+
31
+ When in older browser, pass the locale string as before. (no changes)
30
32
 
31
33
  ```html
32
34
  <script src="/path/to/date-and-time.min.js"></script>
33
35
  <script src="/path/to/locale/zh-cn.js"></script>
34
36
 
35
37
  <script>
36
- date.locale('zh-cn'); // Chinese
38
+ date.locale('zh-cn'); // Chinese
37
39
  date.format(new Date(), 'MMMD日dddd'); // => '1月11日星期一'
38
40
  </script>
39
41
  ```
@@ -41,12 +43,42 @@ date.format(new Date(), 'MMMD日dddd'); // => '1月11日星期一'
41
43
  ### NOTE
42
44
 
43
45
  - You have to import (or require) in advance the all locale modules that you are going to switch to.
44
- - The locale will be actually switched after executing `locale('xx')`.
45
- - You could return the locale to English by executing `locale ('en')`.
46
+ - The locale will be actually switched after executing the `locale()`.
47
+ - You can also change the locale back to English by loading the `en` module:
48
+
49
+ ```javascript
50
+ import en from 'date-and-time/locale/en';
51
+
52
+ date.locale(en);
53
+ ```
54
+
55
+ ### FYI
56
+
57
+ The following (old) methods are deprecated. In the next version it won't be able to use them.
58
+
59
+ - CommonJS:
60
+
61
+ ```javascript
62
+ const date = require('date-and-time');
63
+ require('date-and-time/locale/fr');
64
+
65
+ date.locale('fr'); // French
66
+ date.format(new Date(), 'dddd D MMMM'); // => 'lundi 11 janvier'
67
+ ```
68
+
69
+ - ES Modules (with transpile):
70
+
71
+ ```javascript
72
+ import date from 'date-and-time';
73
+ import 'date-and-time/locale/it';
74
+
75
+ date.locale('it'); // Italian
76
+ date.format(new Date(), 'dddd D MMMM'); // => 'Lunedì 11 gennaio'
77
+ ```
46
78
 
47
- ## Supported List
79
+ ## Supported locale List
48
80
 
49
- For now, it supports the following languages:
81
+ At this time, it supports the following locales:
50
82
 
51
83
  ```text
52
84
  Arabic (ar)
package/PLUGINS.md CHANGED
@@ -1,37 +1,40 @@
1
1
  # Plugins
2
2
 
3
- As this library is oriented toward minimalism, it may seem like a lack of functionality. We think plugin is the most realistic solution for solving such dissatisfaction. By importing the plugins, you could extend the functionality of this library, mainly the formatter and the parser.
4
-
3
+ As this library is oriented toward minimalism, it may seem like a lack of functionality. We think plugin is the most realistic solution for solving such dissatisfaction. By importing the plugins, you can extend the functionality of this library, mainly the formatter and the parser.
5
4
 
6
5
  ## Usage
7
6
 
8
- - Node.js:
7
+ To support `ES Modules` in the next version, the importing method has changed and then the old method has been deprecated.
8
+
9
+ - CommonJS:
9
10
 
10
11
  ```javascript
11
12
  const date = require('date-and-time');
12
- // Import a plugin "foobar".
13
- require('date-and-time/plugin/foobar');
13
+ // Import the plugin "foobar".
14
+ const foobar = require('date-and-time/plugin/foobar');
14
15
 
15
16
  // Apply the plugin to "date-and-time".
16
- date.plugin('foobar');
17
+ date.plugin(foobar);
17
18
  ```
18
19
 
19
- - With a transpiler:
20
+ - ES Modules (with transpile):
20
21
 
21
22
  ```javascript
22
23
  import date from 'date-and-time';
23
- // Import a plugin "foobar".
24
- import 'date-and-time/plugin/foobar';
24
+ // Import the plugin "foobar".
25
+ import foobar from 'date-and-time/plugin/foobar';
25
26
 
26
27
  // Apply the plugin to "date-and-time".
27
- date.plugin('foobar');
28
+ date.plugin(foobar);
28
29
  ```
29
30
 
30
- - The browser:
31
+ - Older browser:
32
+
33
+ When in older browser, pass the plugin name as before. (no changes)
31
34
 
32
35
  ```html
33
36
  <script src="/path/to/date-and-time.min.js"></script>
34
- <!-- Import a plugin "foobar". -->
37
+ <!-- Import the plugin "foobar". -->
35
38
  <script src="/path/to/plugin/foobar.js"></script>
36
39
 
37
40
  <script>
@@ -40,22 +43,82 @@ date.plugin('foobar');
40
43
  </script>
41
44
  ```
42
45
 
46
+ ### FYI
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):
62
+
63
+ ```javascript
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
+ ```
71
+
43
72
  ## Plugin List
44
73
 
74
+ - day-of-week
75
+ - It adds day of week notation to the parser.
76
+
45
77
  - meridiem
46
78
  - It extends `A` token.
47
79
 
80
+ - microsecond
81
+ - It adds microsecond notation to the parser.
82
+
48
83
  - ordinal
49
84
  - It adds ordinal notation of date to the formatter.
50
85
 
86
+ - timespan
87
+ - It adds `timeSpan()` function to the library.
88
+
51
89
  - two-digit-year
52
90
  - It adds two-digit year notation to the parser.
53
91
 
54
92
  ## Plugin Details
55
93
 
94
+ ### day-of-week
95
+
96
+ It adds `dddd`, `ddd` and `dd` tokens to the parser. While these meanings are as follows, in fact they don't make sense because day of week doesn't include information to determine a date:
97
+
98
+ | token | meaning | examples of acceptable form | added or modified |
99
+ |:------|:-------------------------|:----------------------------|:------------------|
100
+ | dddd | day of week (long) | Friday, Sunday | ✔ |
101
+ | ddd | day of week (short) | Fri, Sun | ✔ |
102
+ | dd | day of week (very short) | Fr, Su | ✔ |
103
+
104
+ ```javascript
105
+ const date = require('date-and-time');
106
+ // Import "day-of-week" plugin.
107
+ const day_of_week = require('date-and-time/plugin/day-of-week');
108
+
109
+ // Apply "day-of-week" plugin to `date-and-time`.
110
+ date.plugin(day_of_week);
111
+
112
+ // You can write like this.
113
+ date.parse('Thursday, March 05, 2020', 'dddd, MMMM, D YYYY');
114
+ // You can also write like this, but it is not versatile because length of day of week are variant.
115
+ date.parse('Thursday, March 05, 2020', ' , MMMM, D YYYY');
116
+ date.parse('Friday, March 06, 2020', ' , MMMM, D YYYY');
117
+ ```
118
+
56
119
  ### meridiem
57
120
 
58
- It adds `AA`, `a` and `aa` token to the formatter. These meanings are as follows:
121
+ It adds `AA`, `a` and `aa` tokens to the formatter. These meanings are as follows:
59
122
 
60
123
  | token | meaning | examples of output | added or modified |
61
124
  |:------|:-----------------------------------|:-------------------|:------------------|
@@ -73,10 +136,10 @@ It also extends `A` token of the parser as follows:
73
136
  ```javascript
74
137
  const date = require('date-and-time');
75
138
  // Import "meridiem" plugin.
76
- require('date-and-time/plugin/meridiem');
139
+ const meridiem = require('date-and-time/plugin/meridiem');
77
140
 
78
141
  // Apply "medidiem" plugin to `date-and-time`.
79
- date.plugin('meridiem');
142
+ date.plugin(meridiem);
80
143
 
81
144
  // This is default behavior of the formatter.
82
145
  date.format(new Date(), 'hh:mm A'); // => '12:34 PM'
@@ -93,23 +156,53 @@ date.parse('12:34 pm', 'hh:mm A'); // => Jan 1 1970 12:34:00
93
156
  date.parse('12:34 p.m.', 'hh:mm A'); // => Jan 1 1970 12:34:00
94
157
  ```
95
158
 
159
+ ### microsecond
160
+
161
+ It adds `SSSSSS`, `SSSSS` and `SSSS` tokens to the parser. Thease meanings are as follows:
162
+
163
+ | token | meaning | examples of output | added or modified |
164
+ |:-------|:------------------------------|:-------------------|:------------------|
165
+ | SSSSSS | microsecond (high accuracy) | 753123, 022113 | ✔ |
166
+ | SSSSS | microsecond (middle accuracy) | 75312, 02211 | ✔ |
167
+ | SSSS | microsecond (low accuracy) | 7531, 0221 | ✔ |
168
+ | SSS | millisecond (high accuracy) | 753, 022 | |
169
+ | SS | millisecond (middle accuracy) | 75, 02 | |
170
+ | S | millisecond (low accuracy) | 7, 0 | |
171
+
172
+ ```javascript
173
+ const date = require('date-and-time');
174
+ // Import "microsecond" plugin.
175
+ const microsecond = require('date-and-time/plugin/microsecond');
176
+
177
+ // Apply "microsecond" plugin to `date-and-time`.
178
+ date.plugin(microsecond);
179
+
180
+ // A date object in JavaScript supports `millisecond` (ms):
181
+ date.parse('12:34:56.123', 'HH:mm:ss.SSS');
182
+
183
+ // 4 or more digits number sometimes seen is not `millisecond`, probably `microsecond` (μs):
184
+ date.parse('12:34:56.123456', 'HH:mm:ss.SSSSSS');
185
+
186
+ // 123456µs will be rounded to 123ms.
187
+ ```
188
+
96
189
  ### ordinal
97
190
 
98
191
  It adds `DDD` token to the formatter. This meaning is as follows:
99
192
 
100
193
  | token | meaning | examples of output | added or modified |
101
194
  |:------|:-------------------------|:--------------------|:------------------|
102
- | D | date | 1, 2, 3, 31 | |
103
- | DD | date with zero-padding | 01, 02, 03, 31 | |
104
195
  | DDD | ordinal notation of date | 1st, 2nd, 3rd, 31th | ✔ |
196
+ | DD | date with zero-padding | 01, 02, 03, 31 | |
197
+ | D | date | 1, 2, 3, 31 | |
105
198
 
106
199
  ```javascript
107
200
  const date = require('date-and-time');
108
201
  // Import "ordinal" plugin.
109
- require('date-and-time/plugin/ordinal');
202
+ const ordinal = require('date-and-time/plugin/ordinal');
110
203
 
111
204
  // Apply "ordinal" plugin to `date-and-time`.
112
- date.plugin('ordinal');
205
+ date.plugin(ordinal);
113
206
 
114
207
  // These are default behavior of the formatter.
115
208
  date.format(new Date(), 'MMM D YYYY'); // => Jan 1 2019
@@ -119,6 +212,54 @@ date.format(new Date(), 'MMM DD YYYY'); // => Jan 01 2019
119
212
  date.format(new Date(), 'MMM DDD YYYY'); // => Jan 1st 2019
120
213
  ```
121
214
 
215
+ ### timespan
216
+
217
+ It adds `timeSpan()` function to the library. This function is similar to the `subtract()`, but this can display a formatted elapsed time between two date objects:
218
+
219
+ ```javascript
220
+ const date = require('date-and-time');
221
+ // Import "timespan" plugin.
222
+ const timespan = require('date-and-time/plugin/timespan');
223
+
224
+ // Apply "timespan" plugin to `date-and-time`.
225
+ date.plugin(timespan);
226
+
227
+ const now = new Date(2020, 2, 5, 1, 2, 3, 4);
228
+ const new_years_day = new Date(2020, 0, 1);
229
+
230
+ date.timeSpan(now, new_years_day).toDays('D HH:mm:ss.SSS'); // => '64 01:02:03.004'
231
+ date.timeSpan(now, new_years_day).toHours('H [hours] m [minutes] s [seconds]'); // => '1537 hours 2 minutes 3 seconds'
232
+ date.timeSpan(now, new_years_day).toMinutes('mmmmmmmmmm [minutes]'); // => '0000092222 minutes'
233
+ ```
234
+
235
+ The `timeSpan()` returns an object that has some functions as with the `subtract()`:
236
+
237
+ | function | description |
238
+ |:---------------|:------------------------|
239
+ | toDays | Outputs as dates |
240
+ | toHours | Outputs as hours |
241
+ | toMinutes | Outputs as minutes |
242
+ | toSeconds | Outputs as seconds |
243
+ | toMilliseconds | Outputs as milliseconds |
244
+
245
+ Available tokens in those functions and their meanings are as follows:
246
+
247
+ | function | available tokens |
248
+ |:---------------|:-----------------|
249
+ | toDays | D, H, m, s, S |
250
+ | toHours | H, m, s, S |
251
+ | toMinutes | m, s, S |
252
+ | toSeconds | s, S |
253
+ | toMilliseconds | S |
254
+
255
+ | token | meaning |
256
+ |:------|:------------|
257
+ | D | date |
258
+ | H | 24-hour |
259
+ | m | minute |
260
+ | s | second |
261
+ | S | millisecond |
262
+
122
263
  ### two-digit-year
123
264
 
124
265
  It adds `YY` token to the parser and also changes behavior of `Y` token. These meanings are as follows:
@@ -134,14 +275,14 @@ It adds `YY` token to the parser and also changes behavior of `Y` token. These m
134
275
  ```javascript
135
276
  const date = require('date-and-time');
136
277
  // Import "two-digit-year" plugin.
137
- require('date-and-time/plugin/two-digit-year');
278
+ const two_digit_year = require('date-and-time/plugin/two-digit-year');
138
279
 
139
280
  // These are default behavior of the parser.
140
281
  date.parse('Dec 25 69', 'MMM D YY'); // => Invalid Date
141
282
  date.parse('Dec 25 70', 'MMM D Y'); // => 70 AD (ancient times)
142
283
 
143
284
  // Apply "two-digit-year" plugin to `date-and-time`.
144
- date.plugin('two-digit-year');
285
+ date.plugin(two_digit_year);
145
286
 
146
287
  // These convert the year 69 or earlier to 2000s, the year 70 or later to 1900s.
147
288
  date.parse('Dec 25 69', 'MMM D YY'); // => Dec 25 2069