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/README.md
CHANGED
|
@@ -2,11 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
[](https://circleci.com/gh/knowledgecode/date-and-time)
|
|
4
4
|
|
|
5
|
-
This library is a
|
|
5
|
+
This JS library is just a collection of functions for manipulating date and time. It's small, simple, and easy to learn.
|
|
6
6
|
|
|
7
7
|
## Why
|
|
8
8
|
|
|
9
|
-
JS modules
|
|
9
|
+
Nowadays, JS modules have become huge, complex, and have many dependencies. We think it makes sense to try to keep each module simple and small. Especially for modules that are at the bottom of the dependency chain, such as those dealing with date and time.
|
|
10
10
|
|
|
11
11
|
## Features
|
|
12
12
|
|
|
@@ -18,73 +18,120 @@ JS modules nowadays are getting more huge and complex, and there are also many d
|
|
|
18
18
|
|
|
19
19
|
## Install
|
|
20
20
|
|
|
21
|
-
- via npm:
|
|
22
|
-
|
|
23
21
|
```shell
|
|
24
|
-
npm
|
|
22
|
+
npm i date-and-time
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Recent Changes
|
|
26
|
+
|
|
27
|
+
- 2.0.1
|
|
28
|
+
- Fixed a bug that the timezone plugin does not support changing locales.
|
|
29
|
+
|
|
30
|
+
- 2.0.0
|
|
31
|
+
- Fixed a conflict when importing multiple plugins and locales.
|
|
32
|
+
- **Breaking Changes!** Due to the above fix, the specifications of plugin, locale, and extension have been changed. The `meridiem` plugin and the `two-digit-year` plugin are now partially incompatible with previous ones. See [here](./PLUGINS.md) for details. Also the `extend()` function has changed. If you are using it, check [here](./EXTEND.md) for any impact. The locales are still compatible.
|
|
33
|
+
- Added `timezone` plugin. You can now use the IANA timezone name to output a datetime string or input a date object. See [PLUGINS.md](./PLUGINS.md) for details.
|
|
34
|
+
|
|
35
|
+
- 1.0.1
|
|
36
|
+
- Updated dev dependencies to resolve vulnerability.
|
|
37
|
+
|
|
38
|
+
## Usage
|
|
39
|
+
|
|
40
|
+
- ES Modules:
|
|
41
|
+
|
|
42
|
+
```javascript
|
|
43
|
+
import date from 'date-and-time';
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
- CommonJS:
|
|
47
|
+
|
|
48
|
+
```javascript
|
|
49
|
+
const date = require('date-and-time');
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
- ES Modules for the browser:
|
|
53
|
+
|
|
54
|
+
```html
|
|
55
|
+
<script type="module">
|
|
56
|
+
import date from '/path/to/date-and-time.es.min.js';
|
|
57
|
+
</script>
|
|
25
58
|
```
|
|
26
59
|
|
|
27
|
-
-
|
|
60
|
+
- Older browser:
|
|
28
61
|
|
|
29
62
|
```html
|
|
30
63
|
<script src="/path/to/date-and-time.min.js"></script>
|
|
31
64
|
```
|
|
32
65
|
|
|
33
|
-
|
|
66
|
+
### Note
|
|
34
67
|
|
|
35
|
-
-
|
|
36
|
-
- Fixed regular expression denial of service (ReDoS) vulnerability.
|
|
68
|
+
- 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`.
|
|
37
69
|
|
|
38
|
-
|
|
39
|
-
- Fixed a bug characters inside square brackets `[]` are not validated.
|
|
70
|
+
## API
|
|
40
71
|
|
|
41
|
-
-
|
|
42
|
-
-
|
|
72
|
+
- [format](#formatdateobj-arg-utc)
|
|
73
|
+
- Formatting a Date and Time (Date -> String)
|
|
43
74
|
|
|
44
|
-
|
|
75
|
+
- [parse](#parsedatestring-arg-utc)
|
|
76
|
+
- Parsing a Date and Time string (String -> Date)
|
|
45
77
|
|
|
46
|
-
|
|
78
|
+
- [compile](#compileformatstring)
|
|
79
|
+
- Compiling a format string
|
|
47
80
|
|
|
48
|
-
|
|
81
|
+
- [preparse](#preparsedatestring-arg)
|
|
82
|
+
- Pre-parsing a Date and Time string
|
|
49
83
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
date.transform('3/8/2020', 'D/M/YYYY', 'M/D/YYYY');
|
|
84
|
+
- [isValid](#isvalidarg1-arg2)
|
|
85
|
+
- Validation
|
|
53
86
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
date.format(today, 'M/D/YYYY'); // => '8/3/2020'
|
|
57
|
-
```
|
|
87
|
+
- [transform](#transformdatestring-arg1-arg2-utc)
|
|
88
|
+
- Transforming a Date and Time string (String -> String)
|
|
58
89
|
|
|
59
|
-
|
|
90
|
+
- [addYears](#addyearsdateobj-years)
|
|
91
|
+
- Adding years
|
|
60
92
|
|
|
61
|
-
-
|
|
93
|
+
- [addMonths](#addmonthsdateobj-months)
|
|
94
|
+
- Adding months
|
|
62
95
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
```
|
|
96
|
+
- [addDays](#adddaysdateobj-days)
|
|
97
|
+
- Adding days
|
|
66
98
|
|
|
67
|
-
-
|
|
99
|
+
- [addHours](#addhoursdateobj-hours)
|
|
100
|
+
- Adding hours
|
|
68
101
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
```
|
|
102
|
+
- [addMinutes](#addminutesdateobj-minutes)
|
|
103
|
+
- Adding minutes
|
|
72
104
|
|
|
73
|
-
-
|
|
105
|
+
- [addSeconds](#addsecondsdateobj-seconds)
|
|
106
|
+
- Adding seconds
|
|
74
107
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
```
|
|
108
|
+
- [addMilliseconds](#addmillisecondsdateobj-milliseconds)
|
|
109
|
+
- Adding milliseconds
|
|
78
110
|
|
|
79
|
-
|
|
111
|
+
- [subtract](#subtractdate1-date2)
|
|
112
|
+
- Subtracting two dates
|
|
113
|
+
|
|
114
|
+
- [isLeapYear](#isleapyeary)
|
|
115
|
+
- Whether year is leap year
|
|
116
|
+
|
|
117
|
+
- [isSameDay](#issamedaydate1-date2)
|
|
118
|
+
- Comparison of two dates
|
|
119
|
+
|
|
120
|
+
- [locale](#localecode-locale)
|
|
121
|
+
- Changing the locale or defining new locales
|
|
122
|
+
|
|
123
|
+
- [extend](#extendextension)
|
|
124
|
+
- Feature extension
|
|
80
125
|
|
|
81
|
-
|
|
126
|
+
- [plugin](#pluginname-plugin)
|
|
127
|
+
- Importing or defining plugins
|
|
82
128
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
129
|
+
### format(dateObj, arg[, utc])
|
|
130
|
+
|
|
131
|
+
- @param {**Date**} dateObj - a Date object
|
|
132
|
+
- @param {**string|Array.\<string\>**} arg - a format string or its compiled object
|
|
133
|
+
- @param {**boolean**} [utc] - output as UTC
|
|
134
|
+
- @returns {**string**} a formatted string
|
|
88
135
|
|
|
89
136
|
```javascript
|
|
90
137
|
const now = new Date();
|
|
@@ -136,7 +183,7 @@ You can also use the following tokens by importing plugins. See [PLUGINS.md](./P
|
|
|
136
183
|
| a | meridiem (lowercase) | am, pm |
|
|
137
184
|
| aa | meridiem (lowercase with ellipsis) | a.m., p.m. |
|
|
138
185
|
|
|
139
|
-
####
|
|
186
|
+
#### Note 1. Comments
|
|
140
187
|
|
|
141
188
|
String in parenthese `[...]` in the `formatString` will be ignored as comments:
|
|
142
189
|
|
|
@@ -145,7 +192,7 @@ date.format(new Date(), 'DD-[MM]-YYYY'); // => '02-MM-2015'
|
|
|
145
192
|
date.format(new Date(), '[DD-[MM]-YYYY]'); // => 'DD-[MM]-YYYY'
|
|
146
193
|
```
|
|
147
194
|
|
|
148
|
-
####
|
|
195
|
+
#### Note 2. Output as UTC
|
|
149
196
|
|
|
150
197
|
This function usually outputs a local date-time string. Set to true the `utc` option (the 3rd parameter) if you would like to get a UTC date-time string.
|
|
151
198
|
|
|
@@ -154,17 +201,16 @@ date.format(new Date(), 'hh:mm A [GMT]Z'); // => '11:14 PM GMT-0800'
|
|
|
154
201
|
date.format(new Date(), 'hh:mm A [GMT]Z', true); // => '07:14 AM GMT+0000'
|
|
155
202
|
```
|
|
156
203
|
|
|
157
|
-
####
|
|
204
|
+
#### Note 3. More Tokens
|
|
158
205
|
|
|
159
206
|
You can also define your own tokens. See [EXTEND.md](./EXTEND.md) for details.
|
|
160
207
|
|
|
161
208
|
### parse(dateString, arg[, utc])
|
|
162
209
|
|
|
163
|
-
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
- @returns {**Date**} a constructed date
|
|
210
|
+
- @param {**string**} dateString - a date string
|
|
211
|
+
- @param {**string|Array.\<string\>**} arg - a format string or its compiled object
|
|
212
|
+
- @param {**boolean**} [utc] - input as UTC
|
|
213
|
+
- @returns {**Date**} a constructed date
|
|
168
214
|
|
|
169
215
|
```javascript
|
|
170
216
|
date.parse('2015/01/02 23:14:05', 'YYYY/MM/DD HH:mm:ss'); // => Jan 2 2015 23:14:05 GMT-0800
|
|
@@ -178,45 +224,46 @@ date.parse('Feb 29 2017', 'MMM D YYYY'); // => Invalid Date
|
|
|
178
224
|
|
|
179
225
|
Available tokens and their meanings are as follows:
|
|
180
226
|
|
|
181
|
-
| token | meaning | examples of acceptable form
|
|
182
|
-
|
|
183
|
-
| YYYY | four-digit year | 0999, 2015
|
|
184
|
-
| Y | four-digit year without zero-padding | 2, 44, 88, 2015
|
|
185
|
-
| MMMM | month name (long) | January, December
|
|
186
|
-
| MMM | month name (short) | Jan, Dec
|
|
187
|
-
| MM | month with zero-padding | 01, 12
|
|
188
|
-
| M | month | 1, 12
|
|
189
|
-
| DD | date with zero-padding | 02, 31
|
|
190
|
-
| D | date | 2, 31
|
|
191
|
-
| HH | 24-hour with zero-padding | 23, 08
|
|
192
|
-
| H | 24-hour | 23, 8
|
|
193
|
-
| hh | 12-hour with zero-padding | 11, 08
|
|
194
|
-
| h | 12-hour | 11, 8
|
|
195
|
-
| A | meridiem (uppercase) | AM, PM
|
|
196
|
-
| mm | minute with zero-padding | 14, 07
|
|
197
|
-
| m | minute | 14, 7
|
|
198
|
-
| ss | second with zero-padding | 05, 10
|
|
199
|
-
| s | second | 5, 10
|
|
200
|
-
| SSS | millisecond (high accuracy) | 753, 022
|
|
201
|
-
| SS | millisecond (middle accuracy) | 75, 02
|
|
202
|
-
| S | millisecond (low accuracy) | 7, 0
|
|
203
|
-
| Z | timezone offset | +0100, -0800
|
|
227
|
+
| token | meaning | examples of acceptable form |
|
|
228
|
+
|:-------|:-------------------------------------|:----------------------------|
|
|
229
|
+
| YYYY | four-digit year | 0999, 2015 |
|
|
230
|
+
| Y | four-digit year without zero-padding | 2, 44, 88, 2015 |
|
|
231
|
+
| MMMM | month name (long) | January, December |
|
|
232
|
+
| MMM | month name (short) | Jan, Dec |
|
|
233
|
+
| MM | month with zero-padding | 01, 12 |
|
|
234
|
+
| M | month | 1, 12 |
|
|
235
|
+
| DD | date with zero-padding | 02, 31 |
|
|
236
|
+
| D | date | 2, 31 |
|
|
237
|
+
| HH | 24-hour with zero-padding | 23, 08 |
|
|
238
|
+
| H | 24-hour | 23, 8 |
|
|
239
|
+
| hh | 12-hour with zero-padding | 11, 08 |
|
|
240
|
+
| h | 12-hour | 11, 8 |
|
|
241
|
+
| A | meridiem (uppercase) | AM, PM |
|
|
242
|
+
| mm | minute with zero-padding | 14, 07 |
|
|
243
|
+
| m | minute | 14, 7 |
|
|
244
|
+
| ss | second with zero-padding | 05, 10 |
|
|
245
|
+
| s | second | 5, 10 |
|
|
246
|
+
| SSS | millisecond (high accuracy) | 753, 022 |
|
|
247
|
+
| SS | millisecond (middle accuracy) | 75, 02 |
|
|
248
|
+
| S | millisecond (low accuracy) | 7, 0 |
|
|
249
|
+
| Z | timezone offset | +0100, -0800 |
|
|
204
250
|
|
|
205
251
|
You can also use the following tokens by importing plugins. See [PLUGINS.md](./PLUGINS.md) for details.
|
|
206
252
|
|
|
207
|
-
| token | meaning | examples of acceptable form
|
|
208
|
-
|
|
209
|
-
| YY | two-digit year | 90, 00, 08, 19
|
|
210
|
-
|
|
|
211
|
-
|
|
|
212
|
-
|
|
|
213
|
-
|
|
|
214
|
-
|
|
|
215
|
-
|
|
|
216
|
-
|
|
|
217
|
-
|
|
|
218
|
-
|
|
219
|
-
|
|
253
|
+
| token | meaning | examples of acceptable form |
|
|
254
|
+
|:-------|:-------------------------------------|:----------------------------|
|
|
255
|
+
| YY | two-digit year | 90, 00, 08, 19 |
|
|
256
|
+
| AA | meridiem (uppercase with ellipsis) | A.M., P.M. |
|
|
257
|
+
| a | meridiem (lowercase) | am, pm |
|
|
258
|
+
| aa | meridiem (lowercase with ellipsis) | a.m., p.m. |
|
|
259
|
+
| dddd | day of week (long) | Friday, Sunday |
|
|
260
|
+
| ddd | day of week (short) | Fri, Sun |
|
|
261
|
+
| dd | day of week (very short) | Fr, Su |
|
|
262
|
+
| SSSSSS | microsecond (high accuracy) | 123456, 000001 |
|
|
263
|
+
| SSSSS | microsecond (middle accuracy) | 12345, 00001 |
|
|
264
|
+
| SSSS | microsecond (low accuracy) | 1234, 0001 |
|
|
265
|
+
|
|
266
|
+
#### Note 1. Invalid Date
|
|
220
267
|
|
|
221
268
|
If the function fails to parse, it will return `Invalid Date`. Notice that the `Invalid Date` is a Date object, not `NaN` or `null`. You can tell whether the Date object is invalid as follows:
|
|
222
269
|
|
|
@@ -228,7 +275,7 @@ if (isNaN(today)) {
|
|
|
228
275
|
}
|
|
229
276
|
```
|
|
230
277
|
|
|
231
|
-
####
|
|
278
|
+
#### Note 2. Input as UTC
|
|
232
279
|
|
|
233
280
|
This function usually assumes the `dateString` is a local date-time. Set to true the `utc` option (the 3rd parameter) if it is a UTC date-time.
|
|
234
281
|
|
|
@@ -237,7 +284,7 @@ date.parse('11:14:05 PM', 'hh:mm:ss A'); // => Jan 1 1970 23:14:05 GMT-
|
|
|
237
284
|
date.parse('11:14:05 PM', 'hh:mm:ss A', true); // => Jan 1 1970 23:14:05 GMT+0000 (Jan 1 1970 15:14:05 GMT-0800)
|
|
238
285
|
```
|
|
239
286
|
|
|
240
|
-
####
|
|
287
|
+
#### Note 3. Default Date Time
|
|
241
288
|
|
|
242
289
|
Default date is `January 1, 1970`, time is `00:00:00.000`. Values not passed will be complemented with them:
|
|
243
290
|
|
|
@@ -246,7 +293,7 @@ date.parse('11:14:05 PM', 'hh:mm:ss A'); // => Jan 1 1970 23:14:05 GMT-0800
|
|
|
246
293
|
date.parse('Feb 2000', 'MMM YYYY'); // => Feb 1 2000 00:00:00 GMT-0800
|
|
247
294
|
```
|
|
248
295
|
|
|
249
|
-
####
|
|
296
|
+
#### Note 4. Max Date / Min Date
|
|
250
297
|
|
|
251
298
|
Parsable maximum date is `December 31, 9999`, minimum date is `January 1, 0001`.
|
|
252
299
|
|
|
@@ -258,7 +305,7 @@ date.parse('Jan 1 0001', 'MMM D YYYY'); // => Jan 1 0001 00:00:00 GMT-0800
|
|
|
258
305
|
date.parse('Jan 1 0000', 'MMM D YYYY'); // => Invalid Date
|
|
259
306
|
```
|
|
260
307
|
|
|
261
|
-
####
|
|
308
|
+
#### Note 5. 12-hour notation and Meridiem
|
|
262
309
|
|
|
263
310
|
If use `hh` or `h` (12-hour) token, use together `A` (meridiem) token to get the right value.
|
|
264
311
|
|
|
@@ -267,7 +314,7 @@ date.parse('11:14:05', 'hh:mm:ss'); // => Jan 1 1970 11:14:05 GMT-0800
|
|
|
267
314
|
date.parse('11:14:05 PM', 'hh:mm:ss A'); // => Jan 1 1970 23:14:05 GMT-0800
|
|
268
315
|
```
|
|
269
316
|
|
|
270
|
-
####
|
|
317
|
+
#### Note 6. Token disablement
|
|
271
318
|
|
|
272
319
|
Use square brackets `[]` if a date-time string includes some token characters. Tokens inside square brackets in the `formatString` will be interpreted as normal characters:
|
|
273
320
|
|
|
@@ -276,9 +323,9 @@ date.parse('12 hours 34 minutes', 'HH hours mm minutes'); // => Invalid Da
|
|
|
276
323
|
date.parse('12 hours 34 minutes', 'HH [hours] mm [minutes]'); // => Jan 1 1970 12:34:00 GMT-0800
|
|
277
324
|
```
|
|
278
325
|
|
|
279
|
-
####
|
|
326
|
+
#### Note 7. Wildcard
|
|
280
327
|
|
|
281
|
-
A white space works as a wildcard token. This token is not
|
|
328
|
+
A white space works as a wildcard token. This token is not interpreted into anything. This means it can be ignored a specific variable string. For example, when you would like to ignore a time part from a date string, you can write as follows:
|
|
282
329
|
|
|
283
330
|
```javascript
|
|
284
331
|
// This will be an error.
|
|
@@ -287,9 +334,9 @@ date.parse('2015/01/02 11:14:05', 'YYYY/MM/DD'); // => Invalid Date
|
|
|
287
334
|
date.parse('2015/01/02 11:14:05', 'YYYY/MM/DD '); // => Jan 2 2015 00:00:00 GMT-0800
|
|
288
335
|
```
|
|
289
336
|
|
|
290
|
-
####
|
|
337
|
+
#### Note 8. Ellipsis
|
|
291
338
|
|
|
292
|
-
The parser supports `...` (
|
|
339
|
+
The parser supports `...` (ellipsis) token. The above example can be also written like this:
|
|
293
340
|
|
|
294
341
|
```javascript
|
|
295
342
|
date.parse('2015/01/02 11:14:05', 'YYYY/MM/DD...'); // => Jan 2 2015 00:00:00 GMT-0800
|
|
@@ -297,9 +344,10 @@ date.parse('2015/01/02 11:14:05', 'YYYY/MM/DD...'); // => Jan 2 2015 00:00:00
|
|
|
297
344
|
|
|
298
345
|
### compile(formatString)
|
|
299
346
|
|
|
300
|
-
-
|
|
301
|
-
|
|
302
|
-
|
|
347
|
+
- @param {**string**} formatString - a format string
|
|
348
|
+
- @returns {**Array.\<string\>**} a compiled object
|
|
349
|
+
|
|
350
|
+
If you are going to execute the `format()`, the `parse()` or the `isValid()` so many times with one string format, recommended to precompile and reuse it for performance.
|
|
303
351
|
|
|
304
352
|
```javascript
|
|
305
353
|
const pattern = date.compile('MMM D YYYY h:m:s A');
|
|
@@ -311,14 +359,11 @@ date.parse('2015/01/02 11:14:05', 'YYYY/MM/DD...'); // => Jan 2 2015 00:00:00
|
|
|
311
359
|
date.format(new Date(), pattern); // => Mar 16 2020 6:24:56 PM
|
|
312
360
|
```
|
|
313
361
|
|
|
314
|
-
If you are going to call the `format()`, the `parse()` or the `isValid()` many times with one string format, recommended to precompile and reuse it for performance.
|
|
315
|
-
|
|
316
362
|
### preparse(dateString, arg)
|
|
317
363
|
|
|
318
|
-
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
- @returns {**Object**} a date structure
|
|
364
|
+
- @param {**string**} dateString - a date string
|
|
365
|
+
- @param {**string|Array.\<string\>**} arg - a format string or its compiled object
|
|
366
|
+
- @returns {**Object**} a date structure
|
|
322
367
|
|
|
323
368
|
This function takes exactly the same parameters with the `parse()`, but returns a date structure as follows unlike that:
|
|
324
369
|
|
|
@@ -346,10 +391,9 @@ This date structure provides a parsing result. You will be able to tell from it
|
|
|
346
391
|
|
|
347
392
|
### isValid(arg1[, arg2])
|
|
348
393
|
|
|
349
|
-
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
- @returns {**boolean**} whether the date string is a valid date
|
|
394
|
+
- @param {**Object|string**} arg1 - a date structure or a date string
|
|
395
|
+
- @param {**string|Array.\<string\>**} [arg2] - a format string or its compiled object
|
|
396
|
+
- @returns {**boolean**} whether the date string is a valid date
|
|
353
397
|
|
|
354
398
|
This function takes either exactly the same parameters with the `parse()` or a date structure which the `preparse()` returns, evaluates the validity of them.
|
|
355
399
|
|
|
@@ -365,12 +409,11 @@ date.isValid(result); // => true
|
|
|
365
409
|
|
|
366
410
|
### transform(dateString, arg1, arg2[, utc])
|
|
367
411
|
|
|
368
|
-
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
- @returns {**string**} a formatted string
|
|
412
|
+
- @param {**string**} dateString - a date string
|
|
413
|
+
- @param {**string|Array.\<string\>**} arg1 - a format string or its compiled object
|
|
414
|
+
- @param {**string|Array.\<string\>**} arg2 - a transformed format string or its compiled object
|
|
415
|
+
- @param {**boolean**} [utc] - output as UTC
|
|
416
|
+
- @returns {**string**} a formatted string
|
|
374
417
|
|
|
375
418
|
This function transforms the format of a date string. The 2nd parameter, `arg1`, is the format string of it. Available token list is equal to the `parse()`'s. The 3rd parameter, `arg2`, is the transformed format string. Available token list is equal to the `format()`'s.
|
|
376
419
|
|
|
@@ -384,10 +427,9 @@ date.transform('13:05', 'HH:mm', 'hh:mm A');
|
|
|
384
427
|
|
|
385
428
|
### addYears(dateObj, years)
|
|
386
429
|
|
|
387
|
-
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
- @returns {**Date**} a date after adding the value
|
|
430
|
+
- @param {**Date**} dateObj - a Date object
|
|
431
|
+
- @param {**number**} years - number of years to add
|
|
432
|
+
- @returns {**Date**} a date after adding the value
|
|
391
433
|
|
|
392
434
|
```javascript
|
|
393
435
|
const now = new Date();
|
|
@@ -396,10 +438,9 @@ const next_year = date.addYears(now, 1);
|
|
|
396
438
|
|
|
397
439
|
### addMonths(dateObj, months)
|
|
398
440
|
|
|
399
|
-
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
- @returns {**Date**} a date after adding the value
|
|
441
|
+
- @param {**Date**} dateObj - a Date object
|
|
442
|
+
- @param {**number**} months - number of months to add
|
|
443
|
+
- @returns {**Date**} a date after adding the value
|
|
403
444
|
|
|
404
445
|
```javascript
|
|
405
446
|
const now = new Date();
|
|
@@ -408,10 +449,9 @@ const next_month = date.addMonths(now, 1);
|
|
|
408
449
|
|
|
409
450
|
### addDays(dateObj, days)
|
|
410
451
|
|
|
411
|
-
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
- @returns {**Date**} a date after adding the value
|
|
452
|
+
- @param {**Date**} dateObj - a Date object
|
|
453
|
+
- @param {**number**} days - number of days to add
|
|
454
|
+
- @returns {**Date**} a date after adding the value
|
|
415
455
|
|
|
416
456
|
```javascript
|
|
417
457
|
const now = new Date();
|
|
@@ -420,10 +460,9 @@ const yesterday = date.addDays(now, -1);
|
|
|
420
460
|
|
|
421
461
|
### addHours(dateObj, hours)
|
|
422
462
|
|
|
423
|
-
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
- @returns {**Date**} a date after adding the value
|
|
463
|
+
- @param {**Date**} dateObj - a Date object
|
|
464
|
+
- @param {**number**} hours - number of hours to add
|
|
465
|
+
- @returns {**Date**} a date after adding the value
|
|
427
466
|
|
|
428
467
|
```javascript
|
|
429
468
|
const now = new Date();
|
|
@@ -432,10 +471,9 @@ const an_hour_ago = date.addHours(now, -1);
|
|
|
432
471
|
|
|
433
472
|
### addMinutes(dateObj, minutes)
|
|
434
473
|
|
|
435
|
-
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
- @returns {**Date**} a date after adding the value
|
|
474
|
+
- @param {**Date**} dateObj - a Date object
|
|
475
|
+
- @param {**number**} minutes - number of minutes to add
|
|
476
|
+
- @returns {**Date**} a date after adding the value
|
|
439
477
|
|
|
440
478
|
```javascript
|
|
441
479
|
const now = new Date();
|
|
@@ -444,10 +482,9 @@ const two_minutes_later = date.addMinutes(now, 2);
|
|
|
444
482
|
|
|
445
483
|
### addSeconds(dateObj, seconds)
|
|
446
484
|
|
|
447
|
-
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
- @returns {**Date**} a date after adding the value
|
|
485
|
+
- @param {**Date**} dateObj - a Date object
|
|
486
|
+
- @param {**number**} seconds - number of seconds to add
|
|
487
|
+
- @returns {**Date**} a date after adding the value
|
|
451
488
|
|
|
452
489
|
```javascript
|
|
453
490
|
const now = new Date();
|
|
@@ -456,10 +493,9 @@ const three_seconds_ago = date.addSeconds(now, -3);
|
|
|
456
493
|
|
|
457
494
|
### addMilliseconds(dateObj, milliseconds)
|
|
458
495
|
|
|
459
|
-
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
- @returns {**Date**} a date after adding the value
|
|
496
|
+
- @param {**Date**} dateObj - a Date object
|
|
497
|
+
- @param {**number**} milliseconds - number of milliseconds to add
|
|
498
|
+
- @returns {**Date**} a date after adding the value
|
|
463
499
|
|
|
464
500
|
```javascript
|
|
465
501
|
const now = new Date();
|
|
@@ -468,10 +504,9 @@ const a_millisecond_later = date.addMilliseconds(now, 1);
|
|
|
468
504
|
|
|
469
505
|
### subtract(date1, date2)
|
|
470
506
|
|
|
471
|
-
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
- @returns {**Object**} a result object subtracting date2 from date1
|
|
507
|
+
- @param {**Date**} date1 - a Date object
|
|
508
|
+
- @param {**Date**} date2 - a Date object
|
|
509
|
+
- @returns {**Object**} a result object subtracting date2 from date1
|
|
475
510
|
|
|
476
511
|
```javascript
|
|
477
512
|
const today = new Date(2015, 0, 2);
|
|
@@ -486,9 +521,8 @@ date.subtract(today, yesterday).toMilliseconds(); // => 86400000
|
|
|
486
521
|
|
|
487
522
|
### isLeapYear(y)
|
|
488
523
|
|
|
489
|
-
-
|
|
490
|
-
|
|
491
|
-
- @returns {**boolean**} whether the year is a leap year
|
|
524
|
+
- @param {**number**} y - year
|
|
525
|
+
- @returns {**boolean**} whether year is leap year
|
|
492
526
|
|
|
493
527
|
```javascript
|
|
494
528
|
date.isLeapYear(2015); // => false
|
|
@@ -497,10 +531,9 @@ date.isLeapYear(2012); // => true
|
|
|
497
531
|
|
|
498
532
|
### isSameDay(date1, date2)
|
|
499
533
|
|
|
500
|
-
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
- @returns {**boolean**} whether the dates are the same day (times are ignored)
|
|
534
|
+
- @param {**Date**} date1 - a Date object
|
|
535
|
+
- @param {**Date**} date2 - a Date object
|
|
536
|
+
- @returns {**boolean**} whether the two dates are the same day (time is ignored)
|
|
504
537
|
|
|
505
538
|
```javascript
|
|
506
539
|
const date1 = new Date(2017, 0, 2, 0); // Jan 2 2017 00:00:00
|
|
@@ -512,41 +545,40 @@ date.isSameDay(date1, date3); // => false
|
|
|
512
545
|
|
|
513
546
|
### locale([code[, locale]])
|
|
514
547
|
|
|
515
|
-
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
- @returns {**string**} current language code
|
|
548
|
+
- @param {**Function|string**} [code] - locale installer | language code
|
|
549
|
+
- @param {**Object**} [locale] - locale definition
|
|
550
|
+
- @returns {**string**} current language code
|
|
519
551
|
|
|
520
|
-
It returns
|
|
552
|
+
It returns the current language code if called without any parameters.
|
|
521
553
|
|
|
522
554
|
```javascript
|
|
523
555
|
date.locale(); // => "en"
|
|
524
556
|
```
|
|
525
557
|
|
|
526
|
-
To switch to any other language, call it with a language code.
|
|
558
|
+
To switch to any other language, call it with a locale installer or a language code.
|
|
527
559
|
|
|
528
560
|
```javascript
|
|
529
|
-
date
|
|
561
|
+
import es from 'date-and-time/locale/es';
|
|
562
|
+
|
|
563
|
+
date.locale(es); // Switch to Spanish
|
|
530
564
|
```
|
|
531
565
|
|
|
532
566
|
See [LOCALE.md](./LOCALE.md) for details.
|
|
533
567
|
|
|
534
568
|
### extend(extension)
|
|
535
569
|
|
|
536
|
-
-
|
|
537
|
-
|
|
538
|
-
- @returns {**void**}
|
|
570
|
+
- @param {**Object**} extension - extension object
|
|
571
|
+
- @returns {**void**}
|
|
539
572
|
|
|
540
|
-
|
|
573
|
+
It extends this library. See [EXTEND.md](./EXTEND.md) for details.
|
|
541
574
|
|
|
542
|
-
### plugin(name[,
|
|
575
|
+
### plugin(name[, plugin])
|
|
543
576
|
|
|
544
|
-
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
- @returns {**void**}
|
|
577
|
+
- @param {**Function|string**} name - plugin installer | plugin name
|
|
578
|
+
- @param {**Object**} [plugin] - plugin object
|
|
579
|
+
- @returns {**void**}
|
|
548
580
|
|
|
549
|
-
Plugin is a named
|
|
581
|
+
Plugin is a named extension object. By installing predefined plugins, you can easily extend this library. See [PLUGINS.md](./PLUGINS.md) for details.
|
|
550
582
|
|
|
551
583
|
## Browser Support
|
|
552
584
|
|