date-and-time 4.0.0 → 4.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/README.md +46 -42
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
|
-
|
|
1
|
+
<div align="center">
|
|
2
|
+
<img src="https://raw.githubusercontent.com/knowledgecode/date-and-time/refs/heads/master/logo.png" alt="date-and-time" width="256">
|
|
3
|
+
</div>
|
|
4
|
+
|
|
5
|
+
<div align="center">
|
|
2
6
|
|
|
3
7
|
[](https://github.com/knowledgecode/date-and-time/actions/workflows/test.yml)
|
|
4
|
-
[](https://github.com/knowledgecode/date-and-time/actions/workflows/test.yml)
|
|
5
9
|
[](https://www.npmjs.com/package/date-and-time)
|
|
6
10
|
|
|
7
|
-
|
|
11
|
+
</div>
|
|
8
12
|
|
|
9
13
|
## Install
|
|
10
14
|
|
|
@@ -38,11 +42,11 @@ Version `4.x` has been completely rewritten in TypeScript and some features from
|
|
|
38
42
|
- Tree shaking is now supported
|
|
39
43
|
- Supports `ES2021` and no longer supports older browsers
|
|
40
44
|
|
|
41
|
-
For details, please refer to [MIGRATION.md](docs/MIGRATION.md).
|
|
45
|
+
For details, please refer to [MIGRATION.md](https://github.com/knowledgecode/date-and-time/blob/master/docs/MIGRATION.md).
|
|
42
46
|
|
|
43
47
|
## API
|
|
44
48
|
|
|
45
|
-
|
|
49
|
+
### format(dateObj, arg[, options])
|
|
46
50
|
|
|
47
51
|
<details>
|
|
48
52
|
<summary>Formats a Date object according to the specified format string.</summary>
|
|
@@ -111,7 +115,7 @@ The tokens available for use in the format string specified as the second argume
|
|
|
111
115
|
| Z | Timezone offset | +0100, -0800 |
|
|
112
116
|
| ZZ | Timezone offset with colon | +01:00, -08:00 |
|
|
113
117
|
|
|
114
|
-
Additionally, by importing plugins, you can use the following tokens. For details, please refer to [PLUGINS.md](docs/PLUGINS.md).
|
|
118
|
+
Additionally, by importing plugins, you can use the following tokens. For details, please refer to [PLUGINS.md](https://github.com/knowledgecode/date-and-time/blob/master/docs/PLUGINS.md).
|
|
115
119
|
|
|
116
120
|
| Token | Meaning | Output Examples |
|
|
117
121
|
|:---------|:--------------------------------------------|:----------------------|
|
|
@@ -265,9 +269,9 @@ format(now, 'dddd, D [de] MMMM [de] YYYY, h:mm:ss.SSS aa [GMT]ZZ', { locale: es
|
|
|
265
269
|
</details>
|
|
266
270
|
</details>
|
|
267
271
|
|
|
268
|
-
|
|
272
|
+
#### Notes
|
|
269
273
|
|
|
270
|
-
<details>
|
|
274
|
+
<details open>
|
|
271
275
|
<summary><strong>Comments</strong></summary>
|
|
272
276
|
|
|
273
277
|
Parts of the format string enclosed in brackets are output as-is, regardless of whether they are valid tokens.
|
|
@@ -279,7 +283,7 @@ format(new Date(), '[DD-[MM]-YYYY]'); // => 'DD-[MM]-YYYY'
|
|
|
279
283
|
|
|
280
284
|
</details>
|
|
281
285
|
|
|
282
|
-
<details>
|
|
286
|
+
<details open>
|
|
283
287
|
<summary><strong>Output as UTC timezone</strong></summary>
|
|
284
288
|
|
|
285
289
|
To output date and time as UTC timezone, specify the string `UTC` in the `timeZone` property of `FormatterOptions`.
|
|
@@ -295,7 +299,7 @@ format(new Date(), 'hh:mm A [GMT]Z', { timeZone: 'UTC' });
|
|
|
295
299
|
</details>
|
|
296
300
|
</details>
|
|
297
301
|
|
|
298
|
-
|
|
302
|
+
### parse(dateString, arg[, options])
|
|
299
303
|
|
|
300
304
|
<details>
|
|
301
305
|
<summary>Parses a date string according to the specified format.</summary>
|
|
@@ -364,7 +368,7 @@ The tokens available for use in the format string specified as the second argume
|
|
|
364
368
|
| Z | Timezone offset | +0100, -0800 |
|
|
365
369
|
| ZZ | Timezone offset with colon | +01:00, -08:00 |
|
|
366
370
|
|
|
367
|
-
Additionally, by importing plugins, you can use the following tokens. For details, please refer to [PLUGINS.md](docs/PLUGINS.md).
|
|
371
|
+
Additionally, by importing plugins, you can use the following tokens. For details, please refer to [PLUGINS.md](https://github.com/knowledgecode/date-and-time/blob/master/docs/PLUGINS.md).
|
|
368
372
|
|
|
369
373
|
| Token | Meaning | Input Examples |
|
|
370
374
|
|:----------|:-------------------------------------------|:---------------------|
|
|
@@ -555,9 +559,9 @@ parse(
|
|
|
555
559
|
</details>
|
|
556
560
|
</details>
|
|
557
561
|
|
|
558
|
-
|
|
562
|
+
#### Notes
|
|
559
563
|
|
|
560
|
-
<details>
|
|
564
|
+
<details open>
|
|
561
565
|
<summary><strong>When parsing fails</strong></summary>
|
|
562
566
|
|
|
563
567
|
If this 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:
|
|
@@ -572,7 +576,7 @@ if (isNaN(today.getTime())) {
|
|
|
572
576
|
|
|
573
577
|
</details>
|
|
574
578
|
|
|
575
|
-
<details>
|
|
579
|
+
<details open>
|
|
576
580
|
<summary><strong>Input as UTC timezone</strong></summary>
|
|
577
581
|
|
|
578
582
|
If the `dateString` does not contain a timezone offset and the `timeZone` property of the third argument is not specified, this function considers the `dateString` to be in the local timezone. If you want to process a `dateString` without a timezone offset as UTC timezone, set the string `UTC` to the `timeZone` property in the third argument. Note that the timezone offset contained in the `dateString` takes precedence over the `timeZone` property of the third argument.
|
|
@@ -590,7 +594,7 @@ parse('11:14:05 PM', 'hh:mm:ss A', { timeZone: 'UTC' });
|
|
|
590
594
|
|
|
591
595
|
</details>
|
|
592
596
|
|
|
593
|
-
<details>
|
|
597
|
+
<details open>
|
|
594
598
|
<summary><strong>Default Date Time</strong></summary>
|
|
595
599
|
|
|
596
600
|
Default date is `January 1, 1970`, time is `00:00:00.000`. Any date/time components not specified in the input string will be filled with these default values.
|
|
@@ -605,7 +609,7 @@ parse('Feb 2000', 'MMM YYYY');
|
|
|
605
609
|
|
|
606
610
|
</details>
|
|
607
611
|
|
|
608
|
-
<details>
|
|
612
|
+
<details open>
|
|
609
613
|
<summary><strong>Max Date / Min Date</strong></summary>
|
|
610
614
|
|
|
611
615
|
The parsable maximum date is `December 31, 9999`, and the minimum date is `January 1, 0001`.
|
|
@@ -626,7 +630,7 @@ parse('Jan 1 0000', 'MMM D YYYY');
|
|
|
626
630
|
|
|
627
631
|
</details>
|
|
628
632
|
|
|
629
|
-
<details>
|
|
633
|
+
<details open>
|
|
630
634
|
<summary><strong>12-hour notation and Meridiem</strong></summary>
|
|
631
635
|
|
|
632
636
|
If you use the `hh` or `h` (12-hour) token, use it together with the `A` (meridiem) token to get the correct value.
|
|
@@ -641,7 +645,7 @@ parse('11:14:05 PM', 'hh:mm:ss A');
|
|
|
641
645
|
|
|
642
646
|
</details>
|
|
643
647
|
|
|
644
|
-
<details>
|
|
648
|
+
<details open>
|
|
645
649
|
<summary><strong>Token invalidation</strong></summary>
|
|
646
650
|
|
|
647
651
|
Any part of the given format string that you do not want to be recognized as a token should be enclosed in square brackets. They are considered comments and will not be parsed.
|
|
@@ -656,7 +660,7 @@ parse('12 hours 34 minutes', 'HH [hours] mm [minutes]');
|
|
|
656
660
|
|
|
657
661
|
</details>
|
|
658
662
|
|
|
659
|
-
<details>
|
|
663
|
+
<details open>
|
|
660
664
|
<summary><strong>Wildcard</strong></summary>
|
|
661
665
|
|
|
662
666
|
Whitespace acts as a wildcard token. This token will skip parsing the corresponding parts of the date and time strings. This behavior is similar to enclosing part of a format string in square brackets (Token invalidation), but with the flexibility that the contents do not have to match exactly - only the character count needs to match between the format string and input string.
|
|
@@ -672,7 +676,7 @@ parse('2015/01/02 11:14:05', 'YYYY/MM/DD ');
|
|
|
672
676
|
|
|
673
677
|
</details>
|
|
674
678
|
|
|
675
|
-
<details>
|
|
679
|
+
<details open>
|
|
676
680
|
<summary><strong>Ellipsis</strong></summary>
|
|
677
681
|
|
|
678
682
|
`...` token ignores subsequent corresponding date and time strings. Use this token only at the end of a format string. The above example can also be written like this:
|
|
@@ -685,7 +689,7 @@ parse('2015/01/02 11:14:05', 'YYYY/MM/DD...');
|
|
|
685
689
|
</details>
|
|
686
690
|
</details>
|
|
687
691
|
|
|
688
|
-
|
|
692
|
+
### compile(formatString)
|
|
689
693
|
|
|
690
694
|
<details>
|
|
691
695
|
<summary>Compiles a format string into a tokenized array for efficient parsing and formatting.</summary>
|
|
@@ -711,7 +715,7 @@ format(new Date(), pattern);
|
|
|
711
715
|
|
|
712
716
|
</details>
|
|
713
717
|
|
|
714
|
-
|
|
718
|
+
### preparse(dateString, arg[, options])
|
|
715
719
|
|
|
716
720
|
<details>
|
|
717
721
|
<summary>Preparses a date string according to the specified pattern.</summary>
|
|
@@ -749,7 +753,7 @@ This date structure provides a parsing result. You will be able to tell from it
|
|
|
749
753
|
|
|
750
754
|
</details>
|
|
751
755
|
|
|
752
|
-
|
|
756
|
+
### isValid(dateString, arg[, options])
|
|
753
757
|
|
|
754
758
|
<details>
|
|
755
759
|
<summary>Validates whether a date string is valid according to the specified format.</summary>
|
|
@@ -773,7 +777,7 @@ For details about `ParserOptions`, refer to the `parse` function section.
|
|
|
773
777
|
|
|
774
778
|
</details>
|
|
775
779
|
|
|
776
|
-
|
|
780
|
+
### transform(dateString, arg1, arg2[, options1[, options2]])
|
|
777
781
|
|
|
778
782
|
<details>
|
|
779
783
|
<summary>Transforms a date string from one format to another.</summary>
|
|
@@ -821,7 +825,7 @@ For details about `ParserOptions`, refer to the `parse` function section. For de
|
|
|
821
825
|
|
|
822
826
|
</details>
|
|
823
827
|
|
|
824
|
-
|
|
828
|
+
### addYears(dateObj, years[, timeZone])
|
|
825
829
|
|
|
826
830
|
<details>
|
|
827
831
|
<summary>Adds the specified number of years to a Date object.</summary>
|
|
@@ -856,7 +860,7 @@ const back = addYears(next, -1, 'UTC'); // => Feb 28 2020 (not the origi
|
|
|
856
860
|
|
|
857
861
|
</details>
|
|
858
862
|
|
|
859
|
-
|
|
863
|
+
### addMonths(dateObj, months[, timeZone])
|
|
860
864
|
|
|
861
865
|
<details>
|
|
862
866
|
<summary>Adds the specified number of months to a Date object.</summary>
|
|
@@ -891,7 +895,7 @@ const feb = addMonths(apr, -2, 'UTC'); // => Feb 28 2023
|
|
|
891
895
|
|
|
892
896
|
</details>
|
|
893
897
|
|
|
894
|
-
|
|
898
|
+
### addDays(dateObj, days[, timeZone])
|
|
895
899
|
|
|
896
900
|
<details>
|
|
897
901
|
<summary>Adds the specified number of days to a Date object.</summary>
|
|
@@ -918,7 +922,7 @@ addDays(now, -1, Los_Angeles); // => Mar 10 2024 01:00:00 GMT-08:00
|
|
|
918
922
|
|
|
919
923
|
</details>
|
|
920
924
|
|
|
921
|
-
|
|
925
|
+
### addHours(dateObj, hours)
|
|
922
926
|
|
|
923
927
|
<details>
|
|
924
928
|
<summary>Adds the specified number of hours to a Date object.</summary>
|
|
@@ -940,7 +944,7 @@ addHours(now, -1); // => Jul 23 2025 23:00:00
|
|
|
940
944
|
|
|
941
945
|
</details>
|
|
942
946
|
|
|
943
|
-
|
|
947
|
+
### addMinutes(dateObj, minutes)
|
|
944
948
|
|
|
945
949
|
<details>
|
|
946
950
|
<summary>Adds the specified number of minutes to a Date object.</summary>
|
|
@@ -962,7 +966,7 @@ addMinutes(now, 2); // => Jul 24 2025 00:02:00
|
|
|
962
966
|
|
|
963
967
|
</details>
|
|
964
968
|
|
|
965
|
-
|
|
969
|
+
### addSeconds(dateObj, seconds)
|
|
966
970
|
|
|
967
971
|
<details>
|
|
968
972
|
<summary>Adds the specified number of seconds to a Date object.</summary>
|
|
@@ -984,7 +988,7 @@ addSeconds(now, -3); // => Jul 23 2025 23:59:57
|
|
|
984
988
|
|
|
985
989
|
</details>
|
|
986
990
|
|
|
987
|
-
|
|
991
|
+
### addMilliseconds(dateObj, milliseconds)
|
|
988
992
|
|
|
989
993
|
<details>
|
|
990
994
|
<summary>Adds the specified number of milliseconds to a Date object.</summary>
|
|
@@ -1006,7 +1010,7 @@ addMilliseconds(now, 123); // => Jul 24 2025 00:00:00.123
|
|
|
1006
1010
|
|
|
1007
1011
|
</details>
|
|
1008
1012
|
|
|
1009
|
-
|
|
1013
|
+
### subtract(from, to)
|
|
1010
1014
|
|
|
1011
1015
|
<details>
|
|
1012
1016
|
<summary>Calculates the difference between two dates.</summary>
|
|
@@ -1037,7 +1041,7 @@ duration.toMicroseconds().value; // => 97445006000
|
|
|
1037
1041
|
duration.toNanoseconds().value; // => 97445006000000
|
|
1038
1042
|
```
|
|
1039
1043
|
|
|
1040
|
-
|
|
1044
|
+
#### Duration
|
|
1041
1045
|
|
|
1042
1046
|
The `Duration` object can be directly created not only as a return value of the `subtract` function, but also by passing any numeric value (milliseconds) as a constructor argument.
|
|
1043
1047
|
|
|
@@ -1175,13 +1179,13 @@ duration.toNanoseconds().toParts();
|
|
|
1175
1179
|
|
|
1176
1180
|
</details>
|
|
1177
1181
|
|
|
1178
|
-
|
|
1182
|
+
#### DurationDescriptor
|
|
1179
1183
|
|
|
1180
|
-
|
|
1184
|
+
##### value
|
|
1181
1185
|
|
|
1182
1186
|
The value of the duration in the respective unit.
|
|
1183
1187
|
|
|
1184
|
-
|
|
1188
|
+
##### format(formatString[, numeral])
|
|
1185
1189
|
|
|
1186
1190
|
<details>
|
|
1187
1191
|
<summary>Formats the duration according to the provided format string.</summary>
|
|
@@ -1210,7 +1214,7 @@ What makes the format string in `DurationDescriptor` different from others is th
|
|
|
1210
1214
|
|
|
1211
1215
|
</details>
|
|
1212
1216
|
|
|
1213
|
-
|
|
1217
|
+
##### toParts()
|
|
1214
1218
|
|
|
1215
1219
|
<details>
|
|
1216
1220
|
<summary>Converts the duration to an object containing the parts of the duration in the respective unit.</summary>
|
|
@@ -1229,9 +1233,9 @@ What makes the format string in `DurationDescriptor` different from others is th
|
|
|
1229
1233
|
|
|
1230
1234
|
</details>
|
|
1231
1235
|
|
|
1232
|
-
|
|
1236
|
+
#### Notes
|
|
1233
1237
|
|
|
1234
|
-
<details>
|
|
1238
|
+
<details open>
|
|
1235
1239
|
<summary><strong>Negative Duration</strong></summary>
|
|
1236
1240
|
|
|
1237
1241
|
When the `value` becomes negative, there are slight differences in the output results of the `format` method and the `toParts` method. In the `format` method, only the largest unit in the `DurationDescriptor`, for example, only the `D` token in the case of a `DurationDescriptor` obtained by the `toDays` method, gets a minus sign, while in the `toParts` method, all units get a minus sign.
|
|
@@ -1249,7 +1253,7 @@ duration.toDays().toParts();
|
|
|
1249
1253
|
|
|
1250
1254
|
</details>
|
|
1251
1255
|
|
|
1252
|
-
<details>
|
|
1256
|
+
<details open>
|
|
1253
1257
|
<summary><strong>Negative Zero</strong></summary>
|
|
1254
1258
|
|
|
1255
1259
|
The `format` method may output `-0`. For example, when the value of a `DurationDescriptor` obtained by the `toDays` method is a negative decimal less than 1, using the `D` token in the `format` method outputs `-0`.
|
|
@@ -1268,7 +1272,7 @@ duration.toDays().toParts();
|
|
|
1268
1272
|
</details>
|
|
1269
1273
|
</details>
|
|
1270
1274
|
|
|
1271
|
-
|
|
1275
|
+
### isLeapYear(year)
|
|
1272
1276
|
|
|
1273
1277
|
<details>
|
|
1274
1278
|
<summary>Determines if the specified year is a leap year.</summary>
|
|
@@ -1286,7 +1290,7 @@ isLeapYear(2012); // => true
|
|
|
1286
1290
|
|
|
1287
1291
|
</details>
|
|
1288
1292
|
|
|
1289
|
-
|
|
1293
|
+
### isSameDay(date1, date2)
|
|
1290
1294
|
|
|
1291
1295
|
<details>
|
|
1292
1296
|
<summary>Determines if two dates represent the same calendar day.</summary>
|