date-and-time 4.0.0 → 4.0.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/README.md +53 -45
- package/package.json +28 -8
package/README.md
CHANGED
|
@@ -1,18 +1,26 @@
|
|
|
1
1
|
# date-and-time
|
|
2
2
|
|
|
3
|
+
<div align="center">
|
|
4
|
+
<img src="https://raw.githubusercontent.com/knowledgecode/date-and-time/refs/heads/master/logo.png" alt="date-and-time" width="256">
|
|
5
|
+
</div>
|
|
6
|
+
|
|
7
|
+
<div align="center">
|
|
8
|
+
|
|
3
9
|
[](https://github.com/knowledgecode/date-and-time/actions/workflows/test.yml)
|
|
4
|
-
[](https://github.com/knowledgecode/date-and-time/actions/workflows/test.yml)
|
|
5
11
|
[](https://www.npmjs.com/package/date-and-time)
|
|
6
12
|
|
|
7
|
-
|
|
13
|
+
</div>
|
|
8
14
|
|
|
9
|
-
|
|
15
|
+
The simplest, most intuitive date and time library.
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
10
18
|
|
|
11
19
|
```shell
|
|
12
20
|
npm i date-and-time
|
|
13
21
|
```
|
|
14
22
|
|
|
15
|
-
-
|
|
23
|
+
- ES Modules:
|
|
16
24
|
|
|
17
25
|
```typescript
|
|
18
26
|
import { format } from 'date-and-time';
|
|
@@ -38,11 +46,11 @@ Version `4.x` has been completely rewritten in TypeScript and some features from
|
|
|
38
46
|
- Tree shaking is now supported
|
|
39
47
|
- Supports `ES2021` and no longer supports older browsers
|
|
40
48
|
|
|
41
|
-
For details, please refer to [MIGRATION.md](docs/MIGRATION.md).
|
|
49
|
+
For details, please refer to [MIGRATION.md](https://github.com/knowledgecode/date-and-time/blob/master/docs/MIGRATION.md).
|
|
42
50
|
|
|
43
51
|
## API
|
|
44
52
|
|
|
45
|
-
|
|
53
|
+
### format(dateObj, arg[, options])
|
|
46
54
|
|
|
47
55
|
<details>
|
|
48
56
|
<summary>Formats a Date object according to the specified format string.</summary>
|
|
@@ -111,7 +119,7 @@ The tokens available for use in the format string specified as the second argume
|
|
|
111
119
|
| Z | Timezone offset | +0100, -0800 |
|
|
112
120
|
| ZZ | Timezone offset with colon | +01:00, -08:00 |
|
|
113
121
|
|
|
114
|
-
Additionally, by importing plugins, you can use the following tokens. For details, please refer to [PLUGINS.md](docs/PLUGINS.md).
|
|
122
|
+
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
123
|
|
|
116
124
|
| Token | Meaning | Output Examples |
|
|
117
125
|
|:---------|:--------------------------------------------|:----------------------|
|
|
@@ -265,9 +273,9 @@ format(now, 'dddd, D [de] MMMM [de] YYYY, h:mm:ss.SSS aa [GMT]ZZ', { locale: es
|
|
|
265
273
|
</details>
|
|
266
274
|
</details>
|
|
267
275
|
|
|
268
|
-
|
|
276
|
+
#### Notes
|
|
269
277
|
|
|
270
|
-
<details>
|
|
278
|
+
<details open>
|
|
271
279
|
<summary><strong>Comments</strong></summary>
|
|
272
280
|
|
|
273
281
|
Parts of the format string enclosed in brackets are output as-is, regardless of whether they are valid tokens.
|
|
@@ -279,7 +287,7 @@ format(new Date(), '[DD-[MM]-YYYY]'); // => 'DD-[MM]-YYYY'
|
|
|
279
287
|
|
|
280
288
|
</details>
|
|
281
289
|
|
|
282
|
-
<details>
|
|
290
|
+
<details open>
|
|
283
291
|
<summary><strong>Output as UTC timezone</strong></summary>
|
|
284
292
|
|
|
285
293
|
To output date and time as UTC timezone, specify the string `UTC` in the `timeZone` property of `FormatterOptions`.
|
|
@@ -295,7 +303,7 @@ format(new Date(), 'hh:mm A [GMT]Z', { timeZone: 'UTC' });
|
|
|
295
303
|
</details>
|
|
296
304
|
</details>
|
|
297
305
|
|
|
298
|
-
|
|
306
|
+
### parse(dateString, arg[, options])
|
|
299
307
|
|
|
300
308
|
<details>
|
|
301
309
|
<summary>Parses a date string according to the specified format.</summary>
|
|
@@ -364,7 +372,7 @@ The tokens available for use in the format string specified as the second argume
|
|
|
364
372
|
| Z | Timezone offset | +0100, -0800 |
|
|
365
373
|
| ZZ | Timezone offset with colon | +01:00, -08:00 |
|
|
366
374
|
|
|
367
|
-
Additionally, by importing plugins, you can use the following tokens. For details, please refer to [PLUGINS.md](docs/PLUGINS.md).
|
|
375
|
+
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
376
|
|
|
369
377
|
| Token | Meaning | Input Examples |
|
|
370
378
|
|:----------|:-------------------------------------------|:---------------------|
|
|
@@ -555,9 +563,9 @@ parse(
|
|
|
555
563
|
</details>
|
|
556
564
|
</details>
|
|
557
565
|
|
|
558
|
-
|
|
566
|
+
#### Notes
|
|
559
567
|
|
|
560
|
-
<details>
|
|
568
|
+
<details open>
|
|
561
569
|
<summary><strong>When parsing fails</strong></summary>
|
|
562
570
|
|
|
563
571
|
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 +580,7 @@ if (isNaN(today.getTime())) {
|
|
|
572
580
|
|
|
573
581
|
</details>
|
|
574
582
|
|
|
575
|
-
<details>
|
|
583
|
+
<details open>
|
|
576
584
|
<summary><strong>Input as UTC timezone</strong></summary>
|
|
577
585
|
|
|
578
586
|
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 +598,7 @@ parse('11:14:05 PM', 'hh:mm:ss A', { timeZone: 'UTC' });
|
|
|
590
598
|
|
|
591
599
|
</details>
|
|
592
600
|
|
|
593
|
-
<details>
|
|
601
|
+
<details open>
|
|
594
602
|
<summary><strong>Default Date Time</strong></summary>
|
|
595
603
|
|
|
596
604
|
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 +613,7 @@ parse('Feb 2000', 'MMM YYYY');
|
|
|
605
613
|
|
|
606
614
|
</details>
|
|
607
615
|
|
|
608
|
-
<details>
|
|
616
|
+
<details open>
|
|
609
617
|
<summary><strong>Max Date / Min Date</strong></summary>
|
|
610
618
|
|
|
611
619
|
The parsable maximum date is `December 31, 9999`, and the minimum date is `January 1, 0001`.
|
|
@@ -626,7 +634,7 @@ parse('Jan 1 0000', 'MMM D YYYY');
|
|
|
626
634
|
|
|
627
635
|
</details>
|
|
628
636
|
|
|
629
|
-
<details>
|
|
637
|
+
<details open>
|
|
630
638
|
<summary><strong>12-hour notation and Meridiem</strong></summary>
|
|
631
639
|
|
|
632
640
|
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 +649,7 @@ parse('11:14:05 PM', 'hh:mm:ss A');
|
|
|
641
649
|
|
|
642
650
|
</details>
|
|
643
651
|
|
|
644
|
-
<details>
|
|
652
|
+
<details open>
|
|
645
653
|
<summary><strong>Token invalidation</strong></summary>
|
|
646
654
|
|
|
647
655
|
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 +664,7 @@ parse('12 hours 34 minutes', 'HH [hours] mm [minutes]');
|
|
|
656
664
|
|
|
657
665
|
</details>
|
|
658
666
|
|
|
659
|
-
<details>
|
|
667
|
+
<details open>
|
|
660
668
|
<summary><strong>Wildcard</strong></summary>
|
|
661
669
|
|
|
662
670
|
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 +680,7 @@ parse('2015/01/02 11:14:05', 'YYYY/MM/DD ');
|
|
|
672
680
|
|
|
673
681
|
</details>
|
|
674
682
|
|
|
675
|
-
<details>
|
|
683
|
+
<details open>
|
|
676
684
|
<summary><strong>Ellipsis</strong></summary>
|
|
677
685
|
|
|
678
686
|
`...` 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 +693,7 @@ parse('2015/01/02 11:14:05', 'YYYY/MM/DD...');
|
|
|
685
693
|
</details>
|
|
686
694
|
</details>
|
|
687
695
|
|
|
688
|
-
|
|
696
|
+
### compile(formatString)
|
|
689
697
|
|
|
690
698
|
<details>
|
|
691
699
|
<summary>Compiles a format string into a tokenized array for efficient parsing and formatting.</summary>
|
|
@@ -711,7 +719,7 @@ format(new Date(), pattern);
|
|
|
711
719
|
|
|
712
720
|
</details>
|
|
713
721
|
|
|
714
|
-
|
|
722
|
+
### preparse(dateString, arg[, options])
|
|
715
723
|
|
|
716
724
|
<details>
|
|
717
725
|
<summary>Preparses a date string according to the specified pattern.</summary>
|
|
@@ -749,7 +757,7 @@ This date structure provides a parsing result. You will be able to tell from it
|
|
|
749
757
|
|
|
750
758
|
</details>
|
|
751
759
|
|
|
752
|
-
|
|
760
|
+
### isValid(dateString, arg[, options])
|
|
753
761
|
|
|
754
762
|
<details>
|
|
755
763
|
<summary>Validates whether a date string is valid according to the specified format.</summary>
|
|
@@ -773,7 +781,7 @@ For details about `ParserOptions`, refer to the `parse` function section.
|
|
|
773
781
|
|
|
774
782
|
</details>
|
|
775
783
|
|
|
776
|
-
|
|
784
|
+
### transform(dateString, arg1, arg2[, options1[, options2]])
|
|
777
785
|
|
|
778
786
|
<details>
|
|
779
787
|
<summary>Transforms a date string from one format to another.</summary>
|
|
@@ -821,7 +829,7 @@ For details about `ParserOptions`, refer to the `parse` function section. For de
|
|
|
821
829
|
|
|
822
830
|
</details>
|
|
823
831
|
|
|
824
|
-
|
|
832
|
+
### addYears(dateObj, years[, timeZone])
|
|
825
833
|
|
|
826
834
|
<details>
|
|
827
835
|
<summary>Adds the specified number of years to a Date object.</summary>
|
|
@@ -856,7 +864,7 @@ const back = addYears(next, -1, 'UTC'); // => Feb 28 2020 (not the origi
|
|
|
856
864
|
|
|
857
865
|
</details>
|
|
858
866
|
|
|
859
|
-
|
|
867
|
+
### addMonths(dateObj, months[, timeZone])
|
|
860
868
|
|
|
861
869
|
<details>
|
|
862
870
|
<summary>Adds the specified number of months to a Date object.</summary>
|
|
@@ -891,7 +899,7 @@ const feb = addMonths(apr, -2, 'UTC'); // => Feb 28 2023
|
|
|
891
899
|
|
|
892
900
|
</details>
|
|
893
901
|
|
|
894
|
-
|
|
902
|
+
### addDays(dateObj, days[, timeZone])
|
|
895
903
|
|
|
896
904
|
<details>
|
|
897
905
|
<summary>Adds the specified number of days to a Date object.</summary>
|
|
@@ -918,7 +926,7 @@ addDays(now, -1, Los_Angeles); // => Mar 10 2024 01:00:00 GMT-08:00
|
|
|
918
926
|
|
|
919
927
|
</details>
|
|
920
928
|
|
|
921
|
-
|
|
929
|
+
### addHours(dateObj, hours)
|
|
922
930
|
|
|
923
931
|
<details>
|
|
924
932
|
<summary>Adds the specified number of hours to a Date object.</summary>
|
|
@@ -940,7 +948,7 @@ addHours(now, -1); // => Jul 23 2025 23:00:00
|
|
|
940
948
|
|
|
941
949
|
</details>
|
|
942
950
|
|
|
943
|
-
|
|
951
|
+
### addMinutes(dateObj, minutes)
|
|
944
952
|
|
|
945
953
|
<details>
|
|
946
954
|
<summary>Adds the specified number of minutes to a Date object.</summary>
|
|
@@ -962,7 +970,7 @@ addMinutes(now, 2); // => Jul 24 2025 00:02:00
|
|
|
962
970
|
|
|
963
971
|
</details>
|
|
964
972
|
|
|
965
|
-
|
|
973
|
+
### addSeconds(dateObj, seconds)
|
|
966
974
|
|
|
967
975
|
<details>
|
|
968
976
|
<summary>Adds the specified number of seconds to a Date object.</summary>
|
|
@@ -984,7 +992,7 @@ addSeconds(now, -3); // => Jul 23 2025 23:59:57
|
|
|
984
992
|
|
|
985
993
|
</details>
|
|
986
994
|
|
|
987
|
-
|
|
995
|
+
### addMilliseconds(dateObj, milliseconds)
|
|
988
996
|
|
|
989
997
|
<details>
|
|
990
998
|
<summary>Adds the specified number of milliseconds to a Date object.</summary>
|
|
@@ -1006,7 +1014,7 @@ addMilliseconds(now, 123); // => Jul 24 2025 00:00:00.123
|
|
|
1006
1014
|
|
|
1007
1015
|
</details>
|
|
1008
1016
|
|
|
1009
|
-
|
|
1017
|
+
### subtract(from, to)
|
|
1010
1018
|
|
|
1011
1019
|
<details>
|
|
1012
1020
|
<summary>Calculates the difference between two dates.</summary>
|
|
@@ -1037,7 +1045,7 @@ duration.toMicroseconds().value; // => 97445006000
|
|
|
1037
1045
|
duration.toNanoseconds().value; // => 97445006000000
|
|
1038
1046
|
```
|
|
1039
1047
|
|
|
1040
|
-
|
|
1048
|
+
#### Duration
|
|
1041
1049
|
|
|
1042
1050
|
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
1051
|
|
|
@@ -1175,13 +1183,13 @@ duration.toNanoseconds().toParts();
|
|
|
1175
1183
|
|
|
1176
1184
|
</details>
|
|
1177
1185
|
|
|
1178
|
-
|
|
1186
|
+
#### DurationDescriptor
|
|
1179
1187
|
|
|
1180
|
-
|
|
1188
|
+
##### value
|
|
1181
1189
|
|
|
1182
1190
|
The value of the duration in the respective unit.
|
|
1183
1191
|
|
|
1184
|
-
|
|
1192
|
+
##### format(formatString[, numeral])
|
|
1185
1193
|
|
|
1186
1194
|
<details>
|
|
1187
1195
|
<summary>Formats the duration according to the provided format string.</summary>
|
|
@@ -1210,7 +1218,7 @@ What makes the format string in `DurationDescriptor` different from others is th
|
|
|
1210
1218
|
|
|
1211
1219
|
</details>
|
|
1212
1220
|
|
|
1213
|
-
|
|
1221
|
+
##### toParts()
|
|
1214
1222
|
|
|
1215
1223
|
<details>
|
|
1216
1224
|
<summary>Converts the duration to an object containing the parts of the duration in the respective unit.</summary>
|
|
@@ -1229,9 +1237,9 @@ What makes the format string in `DurationDescriptor` different from others is th
|
|
|
1229
1237
|
|
|
1230
1238
|
</details>
|
|
1231
1239
|
|
|
1232
|
-
|
|
1240
|
+
#### Notes
|
|
1233
1241
|
|
|
1234
|
-
<details>
|
|
1242
|
+
<details open>
|
|
1235
1243
|
<summary><strong>Negative Duration</strong></summary>
|
|
1236
1244
|
|
|
1237
1245
|
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 +1257,7 @@ duration.toDays().toParts();
|
|
|
1249
1257
|
|
|
1250
1258
|
</details>
|
|
1251
1259
|
|
|
1252
|
-
<details>
|
|
1260
|
+
<details open>
|
|
1253
1261
|
<summary><strong>Negative Zero</strong></summary>
|
|
1254
1262
|
|
|
1255
1263
|
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 +1276,7 @@ duration.toDays().toParts();
|
|
|
1268
1276
|
</details>
|
|
1269
1277
|
</details>
|
|
1270
1278
|
|
|
1271
|
-
|
|
1279
|
+
### isLeapYear(year)
|
|
1272
1280
|
|
|
1273
1281
|
<details>
|
|
1274
1282
|
<summary>Determines if the specified year is a leap year.</summary>
|
|
@@ -1286,7 +1294,7 @@ isLeapYear(2012); // => true
|
|
|
1286
1294
|
|
|
1287
1295
|
</details>
|
|
1288
1296
|
|
|
1289
|
-
|
|
1297
|
+
### isSameDay(date1, date2)
|
|
1290
1298
|
|
|
1291
1299
|
<details>
|
|
1292
1300
|
<summary>Determines if two dates represent the same calendar day.</summary>
|
|
@@ -1311,6 +1319,6 @@ isSameDay(date1, date3); // => false
|
|
|
1311
1319
|
|
|
1312
1320
|
</details>
|
|
1313
1321
|
|
|
1314
|
-
##
|
|
1322
|
+
## Contributing
|
|
1315
1323
|
|
|
1316
|
-
|
|
1324
|
+
Contributions are welcome! Please feel free to submit a Pull Request.
|
package/package.json
CHANGED
|
@@ -1,7 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "date-and-time",
|
|
3
|
-
"version": "4.0.
|
|
4
|
-
"description": "The simplest
|
|
3
|
+
"version": "4.0.2",
|
|
4
|
+
"description": "The simplest, most intuitive date and time library",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"date",
|
|
7
|
+
"datetime",
|
|
8
|
+
"duration",
|
|
9
|
+
"format",
|
|
10
|
+
"parse",
|
|
11
|
+
"time",
|
|
12
|
+
"timezone"
|
|
13
|
+
],
|
|
5
14
|
"main": "dist/index.js",
|
|
6
15
|
"types": "dist/index.d.ts",
|
|
7
16
|
"exports": {
|
|
@@ -53,22 +62,33 @@
|
|
|
53
62
|
},
|
|
54
63
|
"author": "KNOWLEDGECODE",
|
|
55
64
|
"license": "MIT",
|
|
65
|
+
"engines": {
|
|
66
|
+
"node": ">=18"
|
|
67
|
+
},
|
|
68
|
+
"homepage": "https://github.com/knowledgecode/date-and-time#readme",
|
|
69
|
+
"bugs": {
|
|
70
|
+
"url": "https://github.com/knowledgecode/date-and-time/issues"
|
|
71
|
+
},
|
|
72
|
+
"repository": {
|
|
73
|
+
"type": "git",
|
|
74
|
+
"url": "git+https://github.com/knowledgecode/date-and-time.git"
|
|
75
|
+
},
|
|
56
76
|
"type": "module",
|
|
77
|
+
"sideEffects": false,
|
|
57
78
|
"devDependencies": {
|
|
58
79
|
"@rollup/plugin-terser": "^0.4.4",
|
|
59
|
-
"@
|
|
60
|
-
"@types/node": "^24.1.0",
|
|
80
|
+
"@types/node": "^24.2.1",
|
|
61
81
|
"@vitest/coverage-v8": "^3.2.4",
|
|
62
|
-
"eslint": "^9.
|
|
82
|
+
"eslint": "^9.33.0",
|
|
63
83
|
"glob": "^11.0.3",
|
|
64
84
|
"globals": "^16.3.0",
|
|
65
85
|
"jiti": "^2.5.1",
|
|
66
86
|
"prettier": "^3.6.2",
|
|
67
|
-
"rollup": "^4.46.
|
|
87
|
+
"rollup": "^4.46.2",
|
|
68
88
|
"rollup-plugin-dts": "^6.2.1",
|
|
69
89
|
"rollup-plugin-esbuild": "^6.2.1",
|
|
70
|
-
"tsx": "^4.20.
|
|
71
|
-
"typescript-eslint": "^8.
|
|
90
|
+
"tsx": "^4.20.4",
|
|
91
|
+
"typescript-eslint": "^8.39.1",
|
|
72
92
|
"vitest": "^3.2.4"
|
|
73
93
|
}
|
|
74
94
|
}
|