chronos-ts 1.0.1 → 1.0.3
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 +4 -3
- package/dist/interval.d.ts +47 -0
- package/dist/interval.js +47 -0
- package/dist/period.d.ts +152 -0
- package/dist/period.js +152 -0
- package/dist/precision.d.ts +15 -0
- package/dist/precision.js +15 -0
- package/dist/utils.d.ts +169 -0
- package/dist/utils.js +169 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -65,7 +65,7 @@ const periodsOverlap = year2023.overlapsWith(q2_2023); // true
|
|
|
65
65
|
|
|
66
66
|
// Get the overlapping period
|
|
67
67
|
const overlap = year2023.overlap(q2_2023);
|
|
68
|
-
console.log(overlap?.getStartDate()
|
|
68
|
+
console.log(overlap?.getStartDate(), overlap?.getEndDate()); // 2023-04-01, 2023-06-30
|
|
69
69
|
|
|
70
70
|
// Subtract a period
|
|
71
71
|
const remainingPeriods = year2023.subtract(q2_2023);
|
|
@@ -186,7 +186,8 @@ Static Methods
|
|
|
186
186
|
#### Enums
|
|
187
187
|
##### Precision
|
|
188
188
|
The Precision enum represents different levels of time precision.
|
|
189
|
-
|
|
189
|
+
```typescript
|
|
190
|
+
enum Precision {
|
|
190
191
|
MINUTE = 'minute',
|
|
191
192
|
HOUR = 'hour',
|
|
192
193
|
DAY = 'day',
|
|
@@ -194,7 +195,7 @@ typescriptCopyenum Precision {
|
|
|
194
195
|
MONTH = 'month',
|
|
195
196
|
YEAR = 'year',
|
|
196
197
|
}
|
|
197
|
-
|
|
198
|
+
```
|
|
198
199
|
### Utility Function
|
|
199
200
|
The package includes various utility functions for working with dates:
|
|
200
201
|
|
package/dist/interval.d.ts
CHANGED
|
@@ -4,11 +4,58 @@ export declare class Interval {
|
|
|
4
4
|
private days;
|
|
5
5
|
private weeks;
|
|
6
6
|
private months;
|
|
7
|
+
/**
|
|
8
|
+
* Constructs a new instance of the Interval class.
|
|
9
|
+
*
|
|
10
|
+
* @param minutes - The number of minutes (default is 0).
|
|
11
|
+
* @param hours - The number of hours (default is 0).
|
|
12
|
+
* @param days - The number of days (default is 0).
|
|
13
|
+
* @param weeks - The number of weeks (default is 0).
|
|
14
|
+
* @param months - The number of months (default is 0).
|
|
15
|
+
*/
|
|
7
16
|
private constructor();
|
|
17
|
+
/**
|
|
18
|
+
* Creates an Interval instance representing the specified number of minutes.
|
|
19
|
+
*
|
|
20
|
+
* @param minutes - The number of minutes for the interval.
|
|
21
|
+
* @returns An Interval instance representing the specified number of minutes.
|
|
22
|
+
*/
|
|
8
23
|
static minutes(minutes: number): Interval;
|
|
24
|
+
/**
|
|
25
|
+
* Creates an Interval instance representing the given number of hours.
|
|
26
|
+
*
|
|
27
|
+
* @param hours - The number of hours for the interval.
|
|
28
|
+
* @returns An Interval instance with the specified hours.
|
|
29
|
+
*/
|
|
9
30
|
static hours(hours: number): Interval;
|
|
31
|
+
/**
|
|
32
|
+
* Creates an Interval instance representing the specified number of days.
|
|
33
|
+
*
|
|
34
|
+
* @param days - The number of days for the interval.
|
|
35
|
+
* @returns An Interval instance with the specified number of days.
|
|
36
|
+
*/
|
|
10
37
|
static days(days: number): Interval;
|
|
38
|
+
/**
|
|
39
|
+
* Creates an Interval instance representing the specified number of weeks.
|
|
40
|
+
*
|
|
41
|
+
* @param weeks - The number of weeks for the interval.
|
|
42
|
+
* @returns An Interval instance with the specified number of weeks.
|
|
43
|
+
*/
|
|
11
44
|
static weeks(weeks: number): Interval;
|
|
45
|
+
/**
|
|
46
|
+
* Creates an Interval instance representing a specified number of months.
|
|
47
|
+
*
|
|
48
|
+
* @param months - The number of months for the interval.
|
|
49
|
+
* @returns An Interval instance with the specified number of months.
|
|
50
|
+
*/
|
|
12
51
|
static months(months: number): Interval;
|
|
52
|
+
/**
|
|
53
|
+
* Calculates the total interval in minutes.
|
|
54
|
+
*
|
|
55
|
+
* This method sums up the minutes, hours, days, weeks, and months
|
|
56
|
+
* to return the total interval in minutes.
|
|
57
|
+
*
|
|
58
|
+
* @returns {number} The total interval in minutes.
|
|
59
|
+
*/
|
|
13
60
|
getMinutesInterval(): number;
|
|
14
61
|
}
|
package/dist/interval.js
CHANGED
|
@@ -2,6 +2,15 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Interval = void 0;
|
|
4
4
|
class Interval {
|
|
5
|
+
/**
|
|
6
|
+
* Constructs a new instance of the Interval class.
|
|
7
|
+
*
|
|
8
|
+
* @param minutes - The number of minutes (default is 0).
|
|
9
|
+
* @param hours - The number of hours (default is 0).
|
|
10
|
+
* @param days - The number of days (default is 0).
|
|
11
|
+
* @param weeks - The number of weeks (default is 0).
|
|
12
|
+
* @param months - The number of months (default is 0).
|
|
13
|
+
*/
|
|
5
14
|
constructor(minutes = 0, hours = 0, days = 0, weeks = 0, months = 0) {
|
|
6
15
|
this.minutes = minutes;
|
|
7
16
|
this.hours = hours;
|
|
@@ -9,21 +18,59 @@ class Interval {
|
|
|
9
18
|
this.weeks = weeks;
|
|
10
19
|
this.months = months;
|
|
11
20
|
}
|
|
21
|
+
/**
|
|
22
|
+
* Creates an Interval instance representing the specified number of minutes.
|
|
23
|
+
*
|
|
24
|
+
* @param minutes - The number of minutes for the interval.
|
|
25
|
+
* @returns An Interval instance representing the specified number of minutes.
|
|
26
|
+
*/
|
|
12
27
|
static minutes(minutes) {
|
|
13
28
|
return new Interval(minutes);
|
|
14
29
|
}
|
|
30
|
+
/**
|
|
31
|
+
* Creates an Interval instance representing the given number of hours.
|
|
32
|
+
*
|
|
33
|
+
* @param hours - The number of hours for the interval.
|
|
34
|
+
* @returns An Interval instance with the specified hours.
|
|
35
|
+
*/
|
|
15
36
|
static hours(hours) {
|
|
16
37
|
return new Interval(0, hours);
|
|
17
38
|
}
|
|
39
|
+
/**
|
|
40
|
+
* Creates an Interval instance representing the specified number of days.
|
|
41
|
+
*
|
|
42
|
+
* @param days - The number of days for the interval.
|
|
43
|
+
* @returns An Interval instance with the specified number of days.
|
|
44
|
+
*/
|
|
18
45
|
static days(days) {
|
|
19
46
|
return new Interval(0, 0, days);
|
|
20
47
|
}
|
|
48
|
+
/**
|
|
49
|
+
* Creates an Interval instance representing the specified number of weeks.
|
|
50
|
+
*
|
|
51
|
+
* @param weeks - The number of weeks for the interval.
|
|
52
|
+
* @returns An Interval instance with the specified number of weeks.
|
|
53
|
+
*/
|
|
21
54
|
static weeks(weeks) {
|
|
22
55
|
return new Interval(0, 0, 0, weeks);
|
|
23
56
|
}
|
|
57
|
+
/**
|
|
58
|
+
* Creates an Interval instance representing a specified number of months.
|
|
59
|
+
*
|
|
60
|
+
* @param months - The number of months for the interval.
|
|
61
|
+
* @returns An Interval instance with the specified number of months.
|
|
62
|
+
*/
|
|
24
63
|
static months(months) {
|
|
25
64
|
return new Interval(0, 0, 0, 0, months);
|
|
26
65
|
}
|
|
66
|
+
/**
|
|
67
|
+
* Calculates the total interval in minutes.
|
|
68
|
+
*
|
|
69
|
+
* This method sums up the minutes, hours, days, weeks, and months
|
|
70
|
+
* to return the total interval in minutes.
|
|
71
|
+
*
|
|
72
|
+
* @returns {number} The total interval in minutes.
|
|
73
|
+
*/
|
|
27
74
|
getMinutesInterval() {
|
|
28
75
|
return (this.minutes +
|
|
29
76
|
this.hours * 60 +
|
package/dist/period.d.ts
CHANGED
|
@@ -5,29 +5,181 @@ export declare class Period {
|
|
|
5
5
|
private endDate;
|
|
6
6
|
private precision;
|
|
7
7
|
private interval;
|
|
8
|
+
/**
|
|
9
|
+
* Constructs a new Period instance.
|
|
10
|
+
*
|
|
11
|
+
* @param start - The start date of the period, either as a string or a Date object.
|
|
12
|
+
* @param end - The end date of the period, either as a string or a Date object.
|
|
13
|
+
* @param precision - The precision level of the period, defaulting to Precision.DAY.
|
|
14
|
+
* @param interval - An optional interval associated with the period, defaulting to null.
|
|
15
|
+
*/
|
|
8
16
|
constructor(start: string | Date, end: string | Date, precision?: Precision, interval?: Interval | null);
|
|
17
|
+
/**
|
|
18
|
+
* Retrieves the start date of the period.
|
|
19
|
+
*
|
|
20
|
+
* @returns {Date} The start date as a Date object.
|
|
21
|
+
*/
|
|
9
22
|
getStartDate(): Date;
|
|
23
|
+
/**
|
|
24
|
+
* Retrieves the end date of the period.
|
|
25
|
+
*
|
|
26
|
+
* @returns {Date} The end date as a Date object.
|
|
27
|
+
*/
|
|
10
28
|
getEndDate(): Date;
|
|
29
|
+
/**
|
|
30
|
+
* Checks if a given date is within the period defined by `startDate` and `endDate`.
|
|
31
|
+
*
|
|
32
|
+
* @param date - The date to check, either as a string or a Date object.
|
|
33
|
+
* @returns `true` if the date is within the period, `false` otherwise.
|
|
34
|
+
*/
|
|
11
35
|
contains(date: string | Date): boolean;
|
|
36
|
+
/**
|
|
37
|
+
* Determines if the current period overlaps with another period.
|
|
38
|
+
*
|
|
39
|
+
* @param other - The other period to compare with.
|
|
40
|
+
* @returns `true` if the periods overlap, otherwise `false`.
|
|
41
|
+
*/
|
|
12
42
|
overlapsWith(other: Period): boolean;
|
|
43
|
+
/**
|
|
44
|
+
* Determines if the current period is adjacent to another period.
|
|
45
|
+
*
|
|
46
|
+
* Two periods are considered adjacent if they do not overlap and the gap
|
|
47
|
+
* between them is within one precision unit of the larger precision of the two periods.
|
|
48
|
+
*
|
|
49
|
+
* @param other - The other period to compare with.
|
|
50
|
+
* @returns `true` if the periods are adjacent, `false` otherwise.
|
|
51
|
+
*/
|
|
13
52
|
isAdjacentTo(other: Period): boolean;
|
|
53
|
+
/**
|
|
54
|
+
* Retrieves an array of dates within the specified interval.
|
|
55
|
+
*
|
|
56
|
+
* @returns {Date[] | null} An array of dates if the interval is defined, otherwise `null`.
|
|
57
|
+
*/
|
|
14
58
|
getDatesInInterval(): Date[] | null;
|
|
59
|
+
/**
|
|
60
|
+
* Calculates the number of minutes in the interval between the start and end dates.
|
|
61
|
+
*
|
|
62
|
+
* @returns {number} The number of minutes between the start and end dates.
|
|
63
|
+
*/
|
|
15
64
|
getMinutesInInterval(): number;
|
|
65
|
+
/**
|
|
66
|
+
* Calculates the number of whole hours in the interval.
|
|
67
|
+
*
|
|
68
|
+
* @returns {number} The number of whole hours in the interval.
|
|
69
|
+
*/
|
|
16
70
|
getHoursInInterval(): number;
|
|
71
|
+
/**
|
|
72
|
+
* Calculates the number of whole days in the interval.
|
|
73
|
+
*
|
|
74
|
+
* @returns The number of whole days in the interval.
|
|
75
|
+
*/
|
|
17
76
|
getDaysInInterval(): number;
|
|
77
|
+
/**
|
|
78
|
+
* Calculates the number of whole weeks in the interval.
|
|
79
|
+
*
|
|
80
|
+
* @returns The number of whole weeks in the interval.
|
|
81
|
+
*/
|
|
18
82
|
getWeeksInInterval(): number;
|
|
83
|
+
/**
|
|
84
|
+
* Calculates the number of whole months in the interval between the start and end dates.
|
|
85
|
+
*
|
|
86
|
+
* This method computes the difference in months between the `startDate` and `endDate` properties.
|
|
87
|
+
* It adjusts for month boundaries by decrementing the month count if the end date's day is less than the start date's day.
|
|
88
|
+
*
|
|
89
|
+
* @returns {number} The number of whole months in the interval.
|
|
90
|
+
*/
|
|
19
91
|
getMonthsInInterval(): number;
|
|
92
|
+
/**
|
|
93
|
+
* Calculates the number of whole years in the interval.
|
|
94
|
+
*
|
|
95
|
+
* @returns The number of whole years in the interval.
|
|
96
|
+
*/
|
|
20
97
|
getYearsInInterval(): number;
|
|
98
|
+
/**
|
|
99
|
+
* Calculates the length of the period in the specified precision.
|
|
100
|
+
*
|
|
101
|
+
* @returns {number} The length of the period, rounded down to the nearest whole number.
|
|
102
|
+
*/
|
|
21
103
|
length(): number;
|
|
104
|
+
/**
|
|
105
|
+
* Determines the overlapping period between this period and another period.
|
|
106
|
+
*
|
|
107
|
+
* @param other - The other period to check for overlap with.
|
|
108
|
+
* @returns The overlapping period as a new `Period` instance, or `null` if there is no overlap.
|
|
109
|
+
*/
|
|
22
110
|
overlap(other: Period): Period | null;
|
|
111
|
+
/**
|
|
112
|
+
* Subtracts the given period from the current period and returns the resulting periods.
|
|
113
|
+
* If the periods do not overlap, the current period is returned as a single-element array.
|
|
114
|
+
* If they do overlap, the resulting periods are returned as an array.
|
|
115
|
+
*
|
|
116
|
+
* @param other - The period to subtract from the current period.
|
|
117
|
+
* @returns An array of resulting periods after subtraction.
|
|
118
|
+
*/
|
|
23
119
|
subtract(other: Period): Period[];
|
|
120
|
+
/**
|
|
121
|
+
* Calculates the gap between this period and another period.
|
|
122
|
+
* If the periods overlap or are adjacent, returns null.
|
|
123
|
+
* Otherwise, returns a new Period representing the gap between the two periods.
|
|
124
|
+
*
|
|
125
|
+
* @param other - The other period to compare with.
|
|
126
|
+
* @returns A new Period representing the gap, or null if the periods overlap or are adjacent.
|
|
127
|
+
*/
|
|
24
128
|
gap(other: Period): Period | null;
|
|
129
|
+
/**
|
|
130
|
+
* Computes the symmetric difference between this period and another period.
|
|
131
|
+
* The symmetric difference is defined as the set of periods that are in either
|
|
132
|
+
* of the two periods but not in their intersection.
|
|
133
|
+
*
|
|
134
|
+
* @param other - The other period to compute the symmetric difference with.
|
|
135
|
+
* @returns An array of periods representing the symmetric difference, sorted
|
|
136
|
+
* by start date and merged if adjacent.
|
|
137
|
+
*/
|
|
25
138
|
symmetricDifference(other: Period): Period[];
|
|
26
139
|
private mergeAdjacentPeriods;
|
|
140
|
+
/**
|
|
141
|
+
* Renews the current period by creating a new `Period` instance.
|
|
142
|
+
* The new period starts immediately after the end of the current period
|
|
143
|
+
* and has the same length and precision.
|
|
144
|
+
*
|
|
145
|
+
* @returns {Period} A new `Period` instance with updated start and end dates.
|
|
146
|
+
*/
|
|
27
147
|
renew(): Period;
|
|
148
|
+
/**
|
|
149
|
+
* Computes the union of this period with another period.
|
|
150
|
+
* If the periods overlap or are adjacent, they are merged into a single period.
|
|
151
|
+
* Otherwise, the periods are returned as separate, sorted periods.
|
|
152
|
+
*
|
|
153
|
+
* @param other - The other period to union with this period.
|
|
154
|
+
* @returns An array of periods resulting from the union operation.
|
|
155
|
+
*/
|
|
28
156
|
union(other: Period): Period[];
|
|
157
|
+
/**
|
|
158
|
+
* Sets the start date for the period.
|
|
159
|
+
*
|
|
160
|
+
* @param start - The start date, which can be a string or a Date object.
|
|
161
|
+
* @returns The current instance for method chaining.
|
|
162
|
+
*/
|
|
29
163
|
setStart(start: string | Date): this;
|
|
164
|
+
/**
|
|
165
|
+
* Sets the end date for the period.
|
|
166
|
+
*
|
|
167
|
+
* @param end - The end date as a string or Date object.
|
|
168
|
+
* @returns The current instance for method chaining.
|
|
169
|
+
*/
|
|
30
170
|
setEnd(end: string | Date): this;
|
|
171
|
+
/**
|
|
172
|
+
* Sets the precision for the period.
|
|
173
|
+
*
|
|
174
|
+
* @param precision - The precision to set.
|
|
175
|
+
* @returns The current instance for method chaining.
|
|
176
|
+
*/
|
|
31
177
|
setPrecision(precision: Precision): this;
|
|
178
|
+
/**
|
|
179
|
+
* Sets the interval for the period.
|
|
180
|
+
*
|
|
181
|
+
* @param interval - The interval to be set.
|
|
182
|
+
* @returns The current instance of the period.
|
|
183
|
+
*/
|
|
32
184
|
setInterval(interval: Interval): this;
|
|
33
185
|
}
|
package/dist/period.js
CHANGED
|
@@ -4,25 +4,64 @@ exports.Period = void 0;
|
|
|
4
4
|
const precision_1 = require("./precision");
|
|
5
5
|
const utils_1 = require("./utils");
|
|
6
6
|
class Period {
|
|
7
|
+
/**
|
|
8
|
+
* Constructs a new Period instance.
|
|
9
|
+
*
|
|
10
|
+
* @param start - The start date of the period, either as a string or a Date object.
|
|
11
|
+
* @param end - The end date of the period, either as a string or a Date object.
|
|
12
|
+
* @param precision - The precision level of the period, defaulting to Precision.DAY.
|
|
13
|
+
* @param interval - An optional interval associated with the period, defaulting to null.
|
|
14
|
+
*/
|
|
7
15
|
constructor(start, end, precision = precision_1.Precision.DAY, interval = null) {
|
|
8
16
|
this.startDate = new Date(start);
|
|
9
17
|
this.endDate = new Date(end);
|
|
10
18
|
this.precision = precision;
|
|
11
19
|
this.interval = interval;
|
|
12
20
|
}
|
|
21
|
+
/**
|
|
22
|
+
* Retrieves the start date of the period.
|
|
23
|
+
*
|
|
24
|
+
* @returns {Date} The start date as a Date object.
|
|
25
|
+
*/
|
|
13
26
|
getStartDate() {
|
|
14
27
|
return new Date(this.startDate);
|
|
15
28
|
}
|
|
29
|
+
/**
|
|
30
|
+
* Retrieves the end date of the period.
|
|
31
|
+
*
|
|
32
|
+
* @returns {Date} The end date as a Date object.
|
|
33
|
+
*/
|
|
16
34
|
getEndDate() {
|
|
17
35
|
return new Date(this.endDate);
|
|
18
36
|
}
|
|
37
|
+
/**
|
|
38
|
+
* Checks if a given date is within the period defined by `startDate` and `endDate`.
|
|
39
|
+
*
|
|
40
|
+
* @param date - The date to check, either as a string or a Date object.
|
|
41
|
+
* @returns `true` if the date is within the period, `false` otherwise.
|
|
42
|
+
*/
|
|
19
43
|
contains(date) {
|
|
20
44
|
const checkDate = new Date(date);
|
|
21
45
|
return checkDate >= this.startDate && checkDate <= this.endDate;
|
|
22
46
|
}
|
|
47
|
+
/**
|
|
48
|
+
* Determines if the current period overlaps with another period.
|
|
49
|
+
*
|
|
50
|
+
* @param other - The other period to compare with.
|
|
51
|
+
* @returns `true` if the periods overlap, otherwise `false`.
|
|
52
|
+
*/
|
|
23
53
|
overlapsWith(other) {
|
|
24
54
|
return this.startDate < other.endDate && this.endDate > other.startDate;
|
|
25
55
|
}
|
|
56
|
+
/**
|
|
57
|
+
* Determines if the current period is adjacent to another period.
|
|
58
|
+
*
|
|
59
|
+
* Two periods are considered adjacent if they do not overlap and the gap
|
|
60
|
+
* between them is within one precision unit of the larger precision of the two periods.
|
|
61
|
+
*
|
|
62
|
+
* @param other - The other period to compare with.
|
|
63
|
+
* @returns `true` if the periods are adjacent, `false` otherwise.
|
|
64
|
+
*/
|
|
26
65
|
isAdjacentTo(other) {
|
|
27
66
|
const thisPrecisionMs = (0, precision_1.getPrecisionInMilliseconds)(this.precision);
|
|
28
67
|
const otherPrecisionMs = (0, precision_1.getPrecisionInMilliseconds)(other.precision);
|
|
@@ -41,24 +80,57 @@ class Period {
|
|
|
41
80
|
return ((gap > 0 && gap <= maxPrecisionMs) ||
|
|
42
81
|
(reverseGap > 0 && reverseGap <= maxPrecisionMs));
|
|
43
82
|
}
|
|
83
|
+
/**
|
|
84
|
+
* Retrieves an array of dates within the specified interval.
|
|
85
|
+
*
|
|
86
|
+
* @returns {Date[] | null} An array of dates if the interval is defined, otherwise `null`.
|
|
87
|
+
*/
|
|
44
88
|
getDatesInInterval() {
|
|
45
89
|
if (this.interval) {
|
|
46
90
|
return (0, utils_1.getDatesWithInterval)(this.startDate, this.endDate, this.interval);
|
|
47
91
|
}
|
|
48
92
|
return null;
|
|
49
93
|
}
|
|
94
|
+
/**
|
|
95
|
+
* Calculates the number of minutes in the interval between the start and end dates.
|
|
96
|
+
*
|
|
97
|
+
* @returns {number} The number of minutes between the start and end dates.
|
|
98
|
+
*/
|
|
50
99
|
getMinutesInInterval() {
|
|
51
100
|
return Math.floor((this.endDate.getTime() - this.startDate.getTime()) / (1000 * 60));
|
|
52
101
|
}
|
|
102
|
+
/**
|
|
103
|
+
* Calculates the number of whole hours in the interval.
|
|
104
|
+
*
|
|
105
|
+
* @returns {number} The number of whole hours in the interval.
|
|
106
|
+
*/
|
|
53
107
|
getHoursInInterval() {
|
|
54
108
|
return Math.floor(this.getMinutesInInterval() / 60);
|
|
55
109
|
}
|
|
110
|
+
/**
|
|
111
|
+
* Calculates the number of whole days in the interval.
|
|
112
|
+
*
|
|
113
|
+
* @returns The number of whole days in the interval.
|
|
114
|
+
*/
|
|
56
115
|
getDaysInInterval() {
|
|
57
116
|
return Math.floor(this.getHoursInInterval() / 24);
|
|
58
117
|
}
|
|
118
|
+
/**
|
|
119
|
+
* Calculates the number of whole weeks in the interval.
|
|
120
|
+
*
|
|
121
|
+
* @returns The number of whole weeks in the interval.
|
|
122
|
+
*/
|
|
59
123
|
getWeeksInInterval() {
|
|
60
124
|
return Math.floor(this.getDaysInInterval() / 7);
|
|
61
125
|
}
|
|
126
|
+
/**
|
|
127
|
+
* Calculates the number of whole months in the interval between the start and end dates.
|
|
128
|
+
*
|
|
129
|
+
* This method computes the difference in months between the `startDate` and `endDate` properties.
|
|
130
|
+
* It adjusts for month boundaries by decrementing the month count if the end date's day is less than the start date's day.
|
|
131
|
+
*
|
|
132
|
+
* @returns {number} The number of whole months in the interval.
|
|
133
|
+
*/
|
|
62
134
|
getMonthsInInterval() {
|
|
63
135
|
let months = (this.endDate.getFullYear() - this.startDate.getFullYear()) * 12;
|
|
64
136
|
months += this.endDate.getMonth() - this.startDate.getMonth();
|
|
@@ -68,13 +140,29 @@ class Period {
|
|
|
68
140
|
}
|
|
69
141
|
return months;
|
|
70
142
|
}
|
|
143
|
+
/**
|
|
144
|
+
* Calculates the number of whole years in the interval.
|
|
145
|
+
*
|
|
146
|
+
* @returns The number of whole years in the interval.
|
|
147
|
+
*/
|
|
71
148
|
getYearsInInterval() {
|
|
72
149
|
return Math.floor(this.getMonthsInInterval() / 12);
|
|
73
150
|
}
|
|
151
|
+
/**
|
|
152
|
+
* Calculates the length of the period in the specified precision.
|
|
153
|
+
*
|
|
154
|
+
* @returns {number} The length of the period, rounded down to the nearest whole number.
|
|
155
|
+
*/
|
|
74
156
|
length() {
|
|
75
157
|
return Math.floor((this.endDate.getTime() - this.startDate.getTime()) /
|
|
76
158
|
(0, precision_1.getPrecisionInMilliseconds)(this.precision));
|
|
77
159
|
}
|
|
160
|
+
/**
|
|
161
|
+
* Determines the overlapping period between this period and another period.
|
|
162
|
+
*
|
|
163
|
+
* @param other - The other period to check for overlap with.
|
|
164
|
+
* @returns The overlapping period as a new `Period` instance, or `null` if there is no overlap.
|
|
165
|
+
*/
|
|
78
166
|
overlap(other) {
|
|
79
167
|
if (!this.overlapsWith(other)) {
|
|
80
168
|
return null;
|
|
@@ -83,6 +171,14 @@ class Period {
|
|
|
83
171
|
const end = new Date(Math.min(this.endDate.getTime(), other.endDate.getTime()));
|
|
84
172
|
return new Period(start, end, this.precision);
|
|
85
173
|
}
|
|
174
|
+
/**
|
|
175
|
+
* Subtracts the given period from the current period and returns the resulting periods.
|
|
176
|
+
* If the periods do not overlap, the current period is returned as a single-element array.
|
|
177
|
+
* If they do overlap, the resulting periods are returned as an array.
|
|
178
|
+
*
|
|
179
|
+
* @param other - The period to subtract from the current period.
|
|
180
|
+
* @returns An array of resulting periods after subtraction.
|
|
181
|
+
*/
|
|
86
182
|
subtract(other) {
|
|
87
183
|
if (!this.overlapsWith(other)) {
|
|
88
184
|
return [this];
|
|
@@ -98,6 +194,14 @@ class Period {
|
|
|
98
194
|
}
|
|
99
195
|
return periods;
|
|
100
196
|
}
|
|
197
|
+
/**
|
|
198
|
+
* Calculates the gap between this period and another period.
|
|
199
|
+
* If the periods overlap or are adjacent, returns null.
|
|
200
|
+
* Otherwise, returns a new Period representing the gap between the two periods.
|
|
201
|
+
*
|
|
202
|
+
* @param other - The other period to compare with.
|
|
203
|
+
* @returns A new Period representing the gap, or null if the periods overlap or are adjacent.
|
|
204
|
+
*/
|
|
101
205
|
gap(other) {
|
|
102
206
|
if (this.overlapsWith(other) || this.isAdjacentTo(other)) {
|
|
103
207
|
return null;
|
|
@@ -107,6 +211,15 @@ class Period {
|
|
|
107
211
|
const end = this.startDate > other.startDate ? this.startDate : other.startDate;
|
|
108
212
|
return new Period(new Date(start.getTime() + precisionMs), new Date(end.getTime() - precisionMs), this.precision);
|
|
109
213
|
}
|
|
214
|
+
/**
|
|
215
|
+
* Computes the symmetric difference between this period and another period.
|
|
216
|
+
* The symmetric difference is defined as the set of periods that are in either
|
|
217
|
+
* of the two periods but not in their intersection.
|
|
218
|
+
*
|
|
219
|
+
* @param other - The other period to compute the symmetric difference with.
|
|
220
|
+
* @returns An array of periods representing the symmetric difference, sorted
|
|
221
|
+
* by start date and merged if adjacent.
|
|
222
|
+
*/
|
|
110
223
|
symmetricDifference(other) {
|
|
111
224
|
const periods = [];
|
|
112
225
|
const overlapPeriod = this.overlap(other);
|
|
@@ -139,12 +252,27 @@ class Period {
|
|
|
139
252
|
}
|
|
140
253
|
return merged;
|
|
141
254
|
}
|
|
255
|
+
/**
|
|
256
|
+
* Renews the current period by creating a new `Period` instance.
|
|
257
|
+
* The new period starts immediately after the end of the current period
|
|
258
|
+
* and has the same length and precision.
|
|
259
|
+
*
|
|
260
|
+
* @returns {Period} A new `Period` instance with updated start and end dates.
|
|
261
|
+
*/
|
|
142
262
|
renew() {
|
|
143
263
|
const length = this.length();
|
|
144
264
|
const newStart = new Date(this.endDate.getTime() + (0, precision_1.getPrecisionInMilliseconds)(this.precision));
|
|
145
265
|
const newEnd = new Date(newStart.getTime() + length * (0, precision_1.getPrecisionInMilliseconds)(this.precision));
|
|
146
266
|
return new Period(newStart, newEnd, this.precision, this.interval);
|
|
147
267
|
}
|
|
268
|
+
/**
|
|
269
|
+
* Computes the union of this period with another period.
|
|
270
|
+
* If the periods overlap or are adjacent, they are merged into a single period.
|
|
271
|
+
* Otherwise, the periods are returned as separate, sorted periods.
|
|
272
|
+
*
|
|
273
|
+
* @param other - The other period to union with this period.
|
|
274
|
+
* @returns An array of periods resulting from the union operation.
|
|
275
|
+
*/
|
|
148
276
|
union(other) {
|
|
149
277
|
if (this.overlapsWith(other) || this.isAdjacentTo(other)) {
|
|
150
278
|
const start = new Date(Math.min(this.startDate.getTime(), other.startDate.getTime()));
|
|
@@ -154,18 +282,42 @@ class Period {
|
|
|
154
282
|
return [this, other].sort((a, b) => a.startDate.getTime() - b.startDate.getTime());
|
|
155
283
|
}
|
|
156
284
|
// Fluent API methods
|
|
285
|
+
/**
|
|
286
|
+
* Sets the start date for the period.
|
|
287
|
+
*
|
|
288
|
+
* @param start - The start date, which can be a string or a Date object.
|
|
289
|
+
* @returns The current instance for method chaining.
|
|
290
|
+
*/
|
|
157
291
|
setStart(start) {
|
|
158
292
|
this.startDate = new Date(start);
|
|
159
293
|
return this;
|
|
160
294
|
}
|
|
295
|
+
/**
|
|
296
|
+
* Sets the end date for the period.
|
|
297
|
+
*
|
|
298
|
+
* @param end - The end date as a string or Date object.
|
|
299
|
+
* @returns The current instance for method chaining.
|
|
300
|
+
*/
|
|
161
301
|
setEnd(end) {
|
|
162
302
|
this.endDate = new Date(end);
|
|
163
303
|
return this;
|
|
164
304
|
}
|
|
305
|
+
/**
|
|
306
|
+
* Sets the precision for the period.
|
|
307
|
+
*
|
|
308
|
+
* @param precision - The precision to set.
|
|
309
|
+
* @returns The current instance for method chaining.
|
|
310
|
+
*/
|
|
165
311
|
setPrecision(precision) {
|
|
166
312
|
this.precision = precision;
|
|
167
313
|
return this;
|
|
168
314
|
}
|
|
315
|
+
/**
|
|
316
|
+
* Sets the interval for the period.
|
|
317
|
+
*
|
|
318
|
+
* @param interval - The interval to be set.
|
|
319
|
+
* @returns The current instance of the period.
|
|
320
|
+
*/
|
|
169
321
|
setInterval(interval) {
|
|
170
322
|
this.interval = interval;
|
|
171
323
|
return this;
|
package/dist/precision.d.ts
CHANGED
|
@@ -6,4 +6,19 @@ export declare enum Precision {
|
|
|
6
6
|
MONTH = "month",
|
|
7
7
|
YEAR = "year"
|
|
8
8
|
}
|
|
9
|
+
/**
|
|
10
|
+
* Converts a given precision to its equivalent duration in milliseconds.
|
|
11
|
+
*
|
|
12
|
+
* @param precision - The precision to convert. It can be one of the following:
|
|
13
|
+
* - `Precision.MINUTE`: 1 minute in milliseconds.
|
|
14
|
+
* - `Precision.HOUR`: 1 hour in milliseconds.
|
|
15
|
+
* - `Precision.DAY`: 1 day in milliseconds.
|
|
16
|
+
* - `Precision.WEEK`: 1 week in milliseconds.
|
|
17
|
+
* - `Precision.MONTH`: 1 month (approximated as 30 days) in milliseconds.
|
|
18
|
+
* - `Precision.YEAR`: 1 year (approximated as 365 days) in milliseconds.
|
|
19
|
+
*
|
|
20
|
+
* @returns The duration in milliseconds corresponding to the given precision.
|
|
21
|
+
*
|
|
22
|
+
* @throws Will throw an error if the provided precision is not supported.
|
|
23
|
+
*/
|
|
9
24
|
export declare function getPrecisionInMilliseconds(precision: Precision): number;
|
package/dist/precision.js
CHANGED
|
@@ -11,6 +11,21 @@ var Precision;
|
|
|
11
11
|
Precision["MONTH"] = "month";
|
|
12
12
|
Precision["YEAR"] = "year";
|
|
13
13
|
})(Precision || (exports.Precision = Precision = {}));
|
|
14
|
+
/**
|
|
15
|
+
* Converts a given precision to its equivalent duration in milliseconds.
|
|
16
|
+
*
|
|
17
|
+
* @param precision - The precision to convert. It can be one of the following:
|
|
18
|
+
* - `Precision.MINUTE`: 1 minute in milliseconds.
|
|
19
|
+
* - `Precision.HOUR`: 1 hour in milliseconds.
|
|
20
|
+
* - `Precision.DAY`: 1 day in milliseconds.
|
|
21
|
+
* - `Precision.WEEK`: 1 week in milliseconds.
|
|
22
|
+
* - `Precision.MONTH`: 1 month (approximated as 30 days) in milliseconds.
|
|
23
|
+
* - `Precision.YEAR`: 1 year (approximated as 365 days) in milliseconds.
|
|
24
|
+
*
|
|
25
|
+
* @returns The duration in milliseconds corresponding to the given precision.
|
|
26
|
+
*
|
|
27
|
+
* @throws Will throw an error if the provided precision is not supported.
|
|
28
|
+
*/
|
|
14
29
|
function getPrecisionInMilliseconds(precision) {
|
|
15
30
|
switch (precision) {
|
|
16
31
|
case Precision.MINUTE:
|
package/dist/utils.d.ts
CHANGED
|
@@ -1,19 +1,188 @@
|
|
|
1
1
|
import { Interval } from './interval';
|
|
2
2
|
import { Precision } from './precision';
|
|
3
|
+
/**
|
|
4
|
+
* Generates an array of dates between the given start and end dates,
|
|
5
|
+
* with a specified interval between each date.
|
|
6
|
+
*
|
|
7
|
+
* @param start - The start date.
|
|
8
|
+
* @param end - The end date.
|
|
9
|
+
* @param interval - An object representing the interval between dates.
|
|
10
|
+
* @returns An array of dates between the start and end dates, inclusive,
|
|
11
|
+
* with the specified interval.
|
|
12
|
+
*/
|
|
3
13
|
export declare function getDatesWithInterval(start: Date, end: Date, interval: Interval): Date[];
|
|
14
|
+
/**
|
|
15
|
+
* Generates an array of dates representing the start of each week within a given interval.
|
|
16
|
+
*
|
|
17
|
+
* @param start - The start date of the interval.
|
|
18
|
+
* @param end - The end date of the interval.
|
|
19
|
+
* @param interval - An object representing the interval in minutes.
|
|
20
|
+
* @returns An array of dates, each representing the start of a week within the interval.
|
|
21
|
+
*/
|
|
4
22
|
export declare function getWeeksWithInterval(start: Date, end: Date, interval: Interval): Date[];
|
|
23
|
+
/**
|
|
24
|
+
* Generates an array of dates between the start and end dates, inclusive,
|
|
25
|
+
* with a specified interval.
|
|
26
|
+
*
|
|
27
|
+
* @param start - The start date.
|
|
28
|
+
* @param end - The end date.
|
|
29
|
+
* @param interval - The interval object that provides the interval in minutes.
|
|
30
|
+
* @returns An array of dates with the specified interval.
|
|
31
|
+
*/
|
|
5
32
|
export declare function getDaysWithInterval(start: Date, end: Date, interval: Interval): Date[];
|
|
33
|
+
/**
|
|
34
|
+
* Generates an array of Date objects representing hours between the start and end dates,
|
|
35
|
+
* incremented by a specified interval.
|
|
36
|
+
*
|
|
37
|
+
* @param start - The start date and time.
|
|
38
|
+
* @param end - The end date and time.
|
|
39
|
+
* @param interval - An object that provides the interval in minutes.
|
|
40
|
+
* @returns An array of Date objects representing the hours within the specified interval.
|
|
41
|
+
*/
|
|
6
42
|
export declare function getHoursWithInterval(start: Date, end: Date, interval: Interval): Date[];
|
|
43
|
+
/**
|
|
44
|
+
* Generates an array of Date objects representing each minute within a specified interval
|
|
45
|
+
* between a start and end date.
|
|
46
|
+
*
|
|
47
|
+
* @param start - The start date and time.
|
|
48
|
+
* @param end - The end date and time.
|
|
49
|
+
* @param interval - An object that provides the interval in minutes.
|
|
50
|
+
* @returns An array of Date objects, each representing a minute within the specified interval.
|
|
51
|
+
*/
|
|
7
52
|
export declare function getMinutesWithInterval(start: Date, end: Date, interval: Interval): Date[];
|
|
53
|
+
/**
|
|
54
|
+
* Generates an array of Date objects representing the start of each month
|
|
55
|
+
* within the specified interval between the start and end dates.
|
|
56
|
+
*
|
|
57
|
+
* @param start - The start date of the interval.
|
|
58
|
+
* @param end - The end date of the interval.
|
|
59
|
+
* @param interval - An object representing the interval in minutes.
|
|
60
|
+
* @returns An array of Date objects, each representing the start of a month within the interval.
|
|
61
|
+
*/
|
|
8
62
|
export declare function getMonthsWithInterval(start: Date, end: Date, interval: Interval): Date[];
|
|
63
|
+
/**
|
|
64
|
+
* Generates an array of Date objects representing the start of each year
|
|
65
|
+
* within the specified interval between the start and end dates.
|
|
66
|
+
*
|
|
67
|
+
* @param start - The start date.
|
|
68
|
+
* @param end - The end date.
|
|
69
|
+
* @param interval - An Interval object that determines the interval in minutes.
|
|
70
|
+
* @returns An array of Date objects representing the start of each year within the interval.
|
|
71
|
+
*/
|
|
9
72
|
export declare function getYearsWithInterval(start: Date, end: Date, interval: Interval): Date[];
|
|
73
|
+
/**
|
|
74
|
+
* Adds a specified amount of time to a given date based on the provided precision unit.
|
|
75
|
+
*
|
|
76
|
+
* @param date - The original date to which the amount will be added.
|
|
77
|
+
* @param amount - The amount of time to add. Can be positive or negative.
|
|
78
|
+
* @param unit - The unit of time to add (e.g., minute, hour, day, week, month, year).
|
|
79
|
+
*
|
|
80
|
+
* @returns A new `Date` object with the specified amount of time added.
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* ```typescript
|
|
84
|
+
* const originalDate = new Date(Date.UTC(2023, 0, 1));
|
|
85
|
+
* const newDate = addToDate(originalDate, 5, Precision.DAY);
|
|
86
|
+
* console.log(newDate); // Outputs: 2023-01-06T00:00:00.000Z
|
|
87
|
+
* ```
|
|
88
|
+
*/
|
|
10
89
|
export declare function addToDate(date: Date, amount: number, unit: Precision): Date;
|
|
90
|
+
/**
|
|
91
|
+
* Subtracts a specified amount of time from a given date.
|
|
92
|
+
*
|
|
93
|
+
* @param date - The original date from which to subtract.
|
|
94
|
+
* @param amount - The amount of time to subtract.
|
|
95
|
+
* @param unit - The unit of time to subtract (e.g., days, months, years).
|
|
96
|
+
* @returns A new Date object with the specified amount of time subtracted.
|
|
97
|
+
*/
|
|
11
98
|
export declare function subtractFromDate(date: Date, amount: number, unit: Precision): Date;
|
|
99
|
+
/**
|
|
100
|
+
* Checks if two dates are on the same calendar day.
|
|
101
|
+
*
|
|
102
|
+
* @param date1 - The first date to compare.
|
|
103
|
+
* @param date2 - The second date to compare.
|
|
104
|
+
* @returns `true` if both dates are on the same day, otherwise `false`.
|
|
105
|
+
*/
|
|
12
106
|
export declare function isSameDay(date1: Date, date2: Date): boolean;
|
|
107
|
+
/**
|
|
108
|
+
* Calculates the ISO week number for a given date.
|
|
109
|
+
*
|
|
110
|
+
* The ISO week date system is a leap week calendar system that is part of the ISO 8601 date and time standard.
|
|
111
|
+
* It assigns a week number to each week of the year, where the first week of the year is the week that contains
|
|
112
|
+
* the first Thursday of the year.
|
|
113
|
+
*
|
|
114
|
+
* @param date - The date object for which to calculate the week number.
|
|
115
|
+
* @returns The ISO week number of the given date.
|
|
116
|
+
*/
|
|
13
117
|
export declare function getWeekNumber(date: Date): number;
|
|
118
|
+
/**
|
|
119
|
+
* Returns the quarter of the year for a given date.
|
|
120
|
+
*
|
|
121
|
+
* @param date - The date object for which to determine the quarter.
|
|
122
|
+
* @returns The quarter of the year (1, 2, 3, or 4).
|
|
123
|
+
*/
|
|
14
124
|
export declare function getQuarter(date: Date): number;
|
|
125
|
+
/**
|
|
126
|
+
* Determines whether a given year is a leap year.
|
|
127
|
+
*
|
|
128
|
+
* A leap year is exactly divisible by 4 except for end-of-century years, which must be divisible by 400.
|
|
129
|
+
* This means that the year 2000 was a leap year, although 1900 was not.
|
|
130
|
+
*
|
|
131
|
+
* @param year - The year to be checked.
|
|
132
|
+
* @returns `true` if the year is a leap year, otherwise `false`.
|
|
133
|
+
*/
|
|
15
134
|
export declare function isLeapYear(year: number): boolean;
|
|
135
|
+
/**
|
|
136
|
+
* Returns the number of days in a given month of a specific year.
|
|
137
|
+
*
|
|
138
|
+
* @param year - The year for which to get the number of days in the month.
|
|
139
|
+
* @param month - The month (0-indexed, where 0 = January, 11 = December) for which to get the number of days.
|
|
140
|
+
* @returns The number of days in the specified month of the given year.
|
|
141
|
+
*/
|
|
16
142
|
export declare function getDaysInMonth(year: number, month: number): number;
|
|
143
|
+
/**
|
|
144
|
+
* Formats a given date according to the specified format string.
|
|
145
|
+
*
|
|
146
|
+
* The format string can contain the following placeholders:
|
|
147
|
+
* - `YYYY`: Full year (e.g., 2023)
|
|
148
|
+
* - `MM`: Month (01-12)
|
|
149
|
+
* - `DD`: Day of the month (01-31)
|
|
150
|
+
* - `HH`: Hours (00-23)
|
|
151
|
+
* - `mm`: Minutes (00-59)
|
|
152
|
+
* - `ss`: Seconds (00-59)
|
|
153
|
+
*
|
|
154
|
+
* Example usage:
|
|
155
|
+
* ```typescript
|
|
156
|
+
* const date = new Date(2023, 0, 1, 12, 30, 45);
|
|
157
|
+
* const formattedDate = formatDate(date, 'YYYY-MM-DD HH:mm:ss');
|
|
158
|
+
* console.log(formattedDate); // Output: "2023-01-01 12:30:45"
|
|
159
|
+
* ```
|
|
160
|
+
*
|
|
161
|
+
* @param date - The date object to format.
|
|
162
|
+
* @param format - The format string.
|
|
163
|
+
* @returns The formatted date string.
|
|
164
|
+
*/
|
|
17
165
|
export declare function formatDate(date: Date, format: string): string;
|
|
166
|
+
/**
|
|
167
|
+
* Parses a date string according to the specified format and returns a Date object.
|
|
168
|
+
*
|
|
169
|
+
* @param dateString - The date string to parse.
|
|
170
|
+
* @param format - The format of the date string. Supported format parts are YYYY, MM, DD, HH, mm, ss.
|
|
171
|
+
* @returns A Date object representing the parsed date.
|
|
172
|
+
*
|
|
173
|
+
* @example
|
|
174
|
+
* ```typescript
|
|
175
|
+
* const date = parseDate('2023-10-05 14:30:00', 'YYYY-MM-DD HH:mm:ss');
|
|
176
|
+
* console.log(date); // Outputs: Thu Oct 05 2023 14:30:00 GMT+0000 (Coordinated Universal Time)
|
|
177
|
+
* ```
|
|
178
|
+
*/
|
|
18
179
|
export declare function parseDate(dateString: string, format: string): Date;
|
|
180
|
+
/**
|
|
181
|
+
* Generates an array of numbers within a specified range.
|
|
182
|
+
*
|
|
183
|
+
* @param start - The starting number of the range.
|
|
184
|
+
* @param end - The ending number of the range.
|
|
185
|
+
* @param step - The step between each number in the range. Defaults to 1.
|
|
186
|
+
* @returns An array of numbers from start to end, incremented by step.
|
|
187
|
+
*/
|
|
19
188
|
export declare function range(start: number, end: number, step?: number): number[];
|
package/dist/utils.js
CHANGED
|
@@ -18,6 +18,16 @@ exports.formatDate = formatDate;
|
|
|
18
18
|
exports.parseDate = parseDate;
|
|
19
19
|
exports.range = range;
|
|
20
20
|
const precision_1 = require("./precision");
|
|
21
|
+
/**
|
|
22
|
+
* Generates an array of dates between the given start and end dates,
|
|
23
|
+
* with a specified interval between each date.
|
|
24
|
+
*
|
|
25
|
+
* @param start - The start date.
|
|
26
|
+
* @param end - The end date.
|
|
27
|
+
* @param interval - An object representing the interval between dates.
|
|
28
|
+
* @returns An array of dates between the start and end dates, inclusive,
|
|
29
|
+
* with the specified interval.
|
|
30
|
+
*/
|
|
21
31
|
function getDatesWithInterval(start, end, interval) {
|
|
22
32
|
const dates = [];
|
|
23
33
|
const current = new Date(start);
|
|
@@ -27,6 +37,14 @@ function getDatesWithInterval(start, end, interval) {
|
|
|
27
37
|
}
|
|
28
38
|
return dates;
|
|
29
39
|
}
|
|
40
|
+
/**
|
|
41
|
+
* Generates an array of dates representing the start of each week within a given interval.
|
|
42
|
+
*
|
|
43
|
+
* @param start - The start date of the interval.
|
|
44
|
+
* @param end - The end date of the interval.
|
|
45
|
+
* @param interval - An object representing the interval in minutes.
|
|
46
|
+
* @returns An array of dates, each representing the start of a week within the interval.
|
|
47
|
+
*/
|
|
30
48
|
function getWeeksWithInterval(start, end, interval) {
|
|
31
49
|
const weeks = [];
|
|
32
50
|
let current = new Date(Date.UTC(start.getUTCFullYear(), start.getUTCMonth(), start.getUTCDate()));
|
|
@@ -41,6 +59,15 @@ function getWeeksWithInterval(start, end, interval) {
|
|
|
41
59
|
}
|
|
42
60
|
return weeks;
|
|
43
61
|
}
|
|
62
|
+
/**
|
|
63
|
+
* Generates an array of dates between the start and end dates, inclusive,
|
|
64
|
+
* with a specified interval.
|
|
65
|
+
*
|
|
66
|
+
* @param start - The start date.
|
|
67
|
+
* @param end - The end date.
|
|
68
|
+
* @param interval - The interval object that provides the interval in minutes.
|
|
69
|
+
* @returns An array of dates with the specified interval.
|
|
70
|
+
*/
|
|
44
71
|
function getDaysWithInterval(start, end, interval) {
|
|
45
72
|
const days = [];
|
|
46
73
|
const current = new Date(start);
|
|
@@ -51,6 +78,15 @@ function getDaysWithInterval(start, end, interval) {
|
|
|
51
78
|
}
|
|
52
79
|
return days;
|
|
53
80
|
}
|
|
81
|
+
/**
|
|
82
|
+
* Generates an array of Date objects representing hours between the start and end dates,
|
|
83
|
+
* incremented by a specified interval.
|
|
84
|
+
*
|
|
85
|
+
* @param start - The start date and time.
|
|
86
|
+
* @param end - The end date and time.
|
|
87
|
+
* @param interval - An object that provides the interval in minutes.
|
|
88
|
+
* @returns An array of Date objects representing the hours within the specified interval.
|
|
89
|
+
*/
|
|
54
90
|
function getHoursWithInterval(start, end, interval) {
|
|
55
91
|
const hours = [];
|
|
56
92
|
const current = new Date(start);
|
|
@@ -61,6 +97,15 @@ function getHoursWithInterval(start, end, interval) {
|
|
|
61
97
|
}
|
|
62
98
|
return hours;
|
|
63
99
|
}
|
|
100
|
+
/**
|
|
101
|
+
* Generates an array of Date objects representing each minute within a specified interval
|
|
102
|
+
* between a start and end date.
|
|
103
|
+
*
|
|
104
|
+
* @param start - The start date and time.
|
|
105
|
+
* @param end - The end date and time.
|
|
106
|
+
* @param interval - An object that provides the interval in minutes.
|
|
107
|
+
* @returns An array of Date objects, each representing a minute within the specified interval.
|
|
108
|
+
*/
|
|
64
109
|
function getMinutesWithInterval(start, end, interval) {
|
|
65
110
|
const minutes = [];
|
|
66
111
|
const current = new Date(start);
|
|
@@ -71,6 +116,15 @@ function getMinutesWithInterval(start, end, interval) {
|
|
|
71
116
|
}
|
|
72
117
|
return minutes;
|
|
73
118
|
}
|
|
119
|
+
/**
|
|
120
|
+
* Generates an array of Date objects representing the start of each month
|
|
121
|
+
* within the specified interval between the start and end dates.
|
|
122
|
+
*
|
|
123
|
+
* @param start - The start date of the interval.
|
|
124
|
+
* @param end - The end date of the interval.
|
|
125
|
+
* @param interval - An object representing the interval in minutes.
|
|
126
|
+
* @returns An array of Date objects, each representing the start of a month within the interval.
|
|
127
|
+
*/
|
|
74
128
|
function getMonthsWithInterval(start, end, interval) {
|
|
75
129
|
const months = [];
|
|
76
130
|
const current = new Date(Date.UTC(start.getUTCFullYear(), start.getUTCMonth(), 1));
|
|
@@ -80,6 +134,15 @@ function getMonthsWithInterval(start, end, interval) {
|
|
|
80
134
|
}
|
|
81
135
|
return months;
|
|
82
136
|
}
|
|
137
|
+
/**
|
|
138
|
+
* Generates an array of Date objects representing the start of each year
|
|
139
|
+
* within the specified interval between the start and end dates.
|
|
140
|
+
*
|
|
141
|
+
* @param start - The start date.
|
|
142
|
+
* @param end - The end date.
|
|
143
|
+
* @param interval - An Interval object that determines the interval in minutes.
|
|
144
|
+
* @returns An array of Date objects representing the start of each year within the interval.
|
|
145
|
+
*/
|
|
83
146
|
function getYearsWithInterval(start, end, interval) {
|
|
84
147
|
const years = [];
|
|
85
148
|
const current = new Date(Date.UTC(start.getUTCFullYear(), 0, 1));
|
|
@@ -91,6 +154,22 @@ function getYearsWithInterval(start, end, interval) {
|
|
|
91
154
|
}
|
|
92
155
|
return years;
|
|
93
156
|
}
|
|
157
|
+
/**
|
|
158
|
+
* Adds a specified amount of time to a given date based on the provided precision unit.
|
|
159
|
+
*
|
|
160
|
+
* @param date - The original date to which the amount will be added.
|
|
161
|
+
* @param amount - The amount of time to add. Can be positive or negative.
|
|
162
|
+
* @param unit - The unit of time to add (e.g., minute, hour, day, week, month, year).
|
|
163
|
+
*
|
|
164
|
+
* @returns A new `Date` object with the specified amount of time added.
|
|
165
|
+
*
|
|
166
|
+
* @example
|
|
167
|
+
* ```typescript
|
|
168
|
+
* const originalDate = new Date(Date.UTC(2023, 0, 1));
|
|
169
|
+
* const newDate = addToDate(originalDate, 5, Precision.DAY);
|
|
170
|
+
* console.log(newDate); // Outputs: 2023-01-06T00:00:00.000Z
|
|
171
|
+
* ```
|
|
172
|
+
*/
|
|
94
173
|
function addToDate(date, amount, unit) {
|
|
95
174
|
const newDate = new Date(Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(), date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds(), date.getUTCMilliseconds()));
|
|
96
175
|
switch (unit) {
|
|
@@ -118,14 +197,39 @@ function addToDate(date, amount, unit) {
|
|
|
118
197
|
}
|
|
119
198
|
return newDate;
|
|
120
199
|
}
|
|
200
|
+
/**
|
|
201
|
+
* Subtracts a specified amount of time from a given date.
|
|
202
|
+
*
|
|
203
|
+
* @param date - The original date from which to subtract.
|
|
204
|
+
* @param amount - The amount of time to subtract.
|
|
205
|
+
* @param unit - The unit of time to subtract (e.g., days, months, years).
|
|
206
|
+
* @returns A new Date object with the specified amount of time subtracted.
|
|
207
|
+
*/
|
|
121
208
|
function subtractFromDate(date, amount, unit) {
|
|
122
209
|
return addToDate(date, -amount, unit);
|
|
123
210
|
}
|
|
211
|
+
/**
|
|
212
|
+
* Checks if two dates are on the same calendar day.
|
|
213
|
+
*
|
|
214
|
+
* @param date1 - The first date to compare.
|
|
215
|
+
* @param date2 - The second date to compare.
|
|
216
|
+
* @returns `true` if both dates are on the same day, otherwise `false`.
|
|
217
|
+
*/
|
|
124
218
|
function isSameDay(date1, date2) {
|
|
125
219
|
return (date1.getFullYear() === date2.getFullYear() &&
|
|
126
220
|
date1.getMonth() === date2.getMonth() &&
|
|
127
221
|
date1.getDate() === date2.getDate());
|
|
128
222
|
}
|
|
223
|
+
/**
|
|
224
|
+
* Calculates the ISO week number for a given date.
|
|
225
|
+
*
|
|
226
|
+
* The ISO week date system is a leap week calendar system that is part of the ISO 8601 date and time standard.
|
|
227
|
+
* It assigns a week number to each week of the year, where the first week of the year is the week that contains
|
|
228
|
+
* the first Thursday of the year.
|
|
229
|
+
*
|
|
230
|
+
* @param date - The date object for which to calculate the week number.
|
|
231
|
+
* @returns The ISO week number of the given date.
|
|
232
|
+
*/
|
|
129
233
|
function getWeekNumber(date) {
|
|
130
234
|
const d = new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()));
|
|
131
235
|
const dayNum = d.getUTCDay() || 7;
|
|
@@ -133,15 +237,59 @@ function getWeekNumber(date) {
|
|
|
133
237
|
const yearStart = new Date(Date.UTC(d.getUTCFullYear(), 0, 1));
|
|
134
238
|
return Math.ceil(((d.getTime() - yearStart.getTime()) / 86400000 + 1) / 7);
|
|
135
239
|
}
|
|
240
|
+
/**
|
|
241
|
+
* Returns the quarter of the year for a given date.
|
|
242
|
+
*
|
|
243
|
+
* @param date - The date object for which to determine the quarter.
|
|
244
|
+
* @returns The quarter of the year (1, 2, 3, or 4).
|
|
245
|
+
*/
|
|
136
246
|
function getQuarter(date) {
|
|
137
247
|
return Math.floor(date.getMonth() / 3) + 1;
|
|
138
248
|
}
|
|
249
|
+
/**
|
|
250
|
+
* Determines whether a given year is a leap year.
|
|
251
|
+
*
|
|
252
|
+
* A leap year is exactly divisible by 4 except for end-of-century years, which must be divisible by 400.
|
|
253
|
+
* This means that the year 2000 was a leap year, although 1900 was not.
|
|
254
|
+
*
|
|
255
|
+
* @param year - The year to be checked.
|
|
256
|
+
* @returns `true` if the year is a leap year, otherwise `false`.
|
|
257
|
+
*/
|
|
139
258
|
function isLeapYear(year) {
|
|
140
259
|
return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
|
|
141
260
|
}
|
|
261
|
+
/**
|
|
262
|
+
* Returns the number of days in a given month of a specific year.
|
|
263
|
+
*
|
|
264
|
+
* @param year - The year for which to get the number of days in the month.
|
|
265
|
+
* @param month - The month (0-indexed, where 0 = January, 11 = December) for which to get the number of days.
|
|
266
|
+
* @returns The number of days in the specified month of the given year.
|
|
267
|
+
*/
|
|
142
268
|
function getDaysInMonth(year, month) {
|
|
143
269
|
return new Date(year, month + 1, 0).getDate();
|
|
144
270
|
}
|
|
271
|
+
/**
|
|
272
|
+
* Formats a given date according to the specified format string.
|
|
273
|
+
*
|
|
274
|
+
* The format string can contain the following placeholders:
|
|
275
|
+
* - `YYYY`: Full year (e.g., 2023)
|
|
276
|
+
* - `MM`: Month (01-12)
|
|
277
|
+
* - `DD`: Day of the month (01-31)
|
|
278
|
+
* - `HH`: Hours (00-23)
|
|
279
|
+
* - `mm`: Minutes (00-59)
|
|
280
|
+
* - `ss`: Seconds (00-59)
|
|
281
|
+
*
|
|
282
|
+
* Example usage:
|
|
283
|
+
* ```typescript
|
|
284
|
+
* const date = new Date(2023, 0, 1, 12, 30, 45);
|
|
285
|
+
* const formattedDate = formatDate(date, 'YYYY-MM-DD HH:mm:ss');
|
|
286
|
+
* console.log(formattedDate); // Output: "2023-01-01 12:30:45"
|
|
287
|
+
* ```
|
|
288
|
+
*
|
|
289
|
+
* @param date - The date object to format.
|
|
290
|
+
* @param format - The format string.
|
|
291
|
+
* @returns The formatted date string.
|
|
292
|
+
*/
|
|
145
293
|
function formatDate(date, format) {
|
|
146
294
|
const pad = (n) => n.toString().padStart(2, '0');
|
|
147
295
|
const map = {
|
|
@@ -154,6 +302,19 @@ function formatDate(date, format) {
|
|
|
154
302
|
};
|
|
155
303
|
return format.replace(/YYYY|MM|DD|HH|mm|ss/gi, (matched) => map[matched]);
|
|
156
304
|
}
|
|
305
|
+
/**
|
|
306
|
+
* Parses a date string according to the specified format and returns a Date object.
|
|
307
|
+
*
|
|
308
|
+
* @param dateString - The date string to parse.
|
|
309
|
+
* @param format - The format of the date string. Supported format parts are YYYY, MM, DD, HH, mm, ss.
|
|
310
|
+
* @returns A Date object representing the parsed date.
|
|
311
|
+
*
|
|
312
|
+
* @example
|
|
313
|
+
* ```typescript
|
|
314
|
+
* const date = parseDate('2023-10-05 14:30:00', 'YYYY-MM-DD HH:mm:ss');
|
|
315
|
+
* console.log(date); // Outputs: Thu Oct 05 2023 14:30:00 GMT+0000 (Coordinated Universal Time)
|
|
316
|
+
* ```
|
|
317
|
+
*/
|
|
157
318
|
function parseDate(dateString, format) {
|
|
158
319
|
const map = {};
|
|
159
320
|
const formatParts = format.match(/YYYY|MM|DD|HH|mm|ss/gi) || [];
|
|
@@ -163,6 +324,14 @@ function parseDate(dateString, format) {
|
|
|
163
324
|
});
|
|
164
325
|
return new Date(map['YYYY'] || 0, (map['MM'] || 1) - 1, map['DD'] || 1, map['HH'] || 0, map['mm'] || 0, map['ss'] || 0);
|
|
165
326
|
}
|
|
327
|
+
/**
|
|
328
|
+
* Generates an array of numbers within a specified range.
|
|
329
|
+
*
|
|
330
|
+
* @param start - The starting number of the range.
|
|
331
|
+
* @param end - The ending number of the range.
|
|
332
|
+
* @param step - The step between each number in the range. Defaults to 1.
|
|
333
|
+
* @returns An array of numbers from start to end, incremented by step.
|
|
334
|
+
*/
|
|
166
335
|
function range(start, end, step = 1) {
|
|
167
336
|
return Array.from({ length: Math.floor((end - start) / step) + 1 }, (_, i) => start + i * step);
|
|
168
337
|
}
|
package/package.json
CHANGED