ts-time-utils 3.0.4 → 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 +91 -6
- package/dist/calculate.d.ts +25 -0
- package/dist/calculate.d.ts.map +1 -1
- package/dist/calculate.js +125 -0
- package/dist/calendar.d.ts +45 -0
- package/dist/calendar.d.ts.map +1 -1
- package/dist/calendar.js +68 -0
- package/dist/calendars.d.ts +156 -0
- package/dist/calendars.d.ts.map +1 -0
- package/dist/calendars.js +348 -0
- package/dist/compare.d.ts +27 -0
- package/dist/compare.d.ts.map +1 -1
- package/dist/compare.js +46 -0
- package/dist/esm/calculate.d.ts +25 -0
- package/dist/esm/calculate.d.ts.map +1 -1
- package/dist/esm/calculate.js +125 -0
- package/dist/esm/calendar.d.ts +45 -0
- package/dist/esm/calendar.d.ts.map +1 -1
- package/dist/esm/calendar.js +68 -0
- package/dist/esm/calendars.d.ts +156 -0
- package/dist/esm/calendars.d.ts.map +1 -0
- package/dist/esm/calendars.js +348 -0
- package/dist/esm/compare.d.ts +27 -0
- package/dist/esm/compare.d.ts.map +1 -1
- package/dist/esm/compare.js +46 -0
- package/dist/esm/holidays.d.ts +11 -1
- package/dist/esm/holidays.d.ts.map +1 -1
- package/dist/esm/holidays.js +220 -1
- package/dist/esm/index.d.ts +13 -7
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +17 -9
- package/dist/esm/iterate.d.ts +55 -0
- package/dist/esm/iterate.d.ts.map +1 -1
- package/dist/esm/iterate.js +86 -0
- package/dist/esm/locale.d.ts +53 -0
- package/dist/esm/locale.d.ts.map +1 -1
- package/dist/esm/locale.js +141 -0
- package/dist/esm/precision.d.ts +225 -0
- package/dist/esm/precision.d.ts.map +1 -0
- package/dist/esm/precision.js +491 -0
- package/dist/esm/temporal.d.ts +237 -0
- package/dist/esm/temporal.d.ts.map +1 -0
- package/dist/esm/temporal.js +660 -0
- package/dist/esm/validate.d.ts +30 -0
- package/dist/esm/validate.d.ts.map +1 -1
- package/dist/esm/validate.js +48 -0
- package/dist/holidays.d.ts +11 -1
- package/dist/holidays.d.ts.map +1 -1
- package/dist/holidays.js +220 -1
- package/dist/index.d.ts +13 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +17 -9
- package/dist/iterate.d.ts +55 -0
- package/dist/iterate.d.ts.map +1 -1
- package/dist/iterate.js +86 -0
- package/dist/locale.d.ts +53 -0
- package/dist/locale.d.ts.map +1 -1
- package/dist/locale.js +141 -0
- package/dist/precision.d.ts +225 -0
- package/dist/precision.d.ts.map +1 -0
- package/dist/precision.js +491 -0
- package/dist/temporal.d.ts +237 -0
- package/dist/temporal.d.ts.map +1 -0
- package/dist/temporal.js +660 -0
- package/dist/validate.d.ts +30 -0
- package/dist/validate.d.ts.map +1 -1
- package/dist/validate.js +48 -0
- package/package.json +16 -1
|
@@ -0,0 +1,348 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Non-Gregorian calendar conversions using Intl.DateTimeFormat
|
|
3
|
+
* Supports Hebrew, Islamic, Buddhist, Japanese, Persian, and Chinese calendars
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Extracts calendar date parts using Intl.DateTimeFormat
|
|
7
|
+
*/
|
|
8
|
+
function extractCalendarParts(date, calendar) {
|
|
9
|
+
// First get numeric values where possible
|
|
10
|
+
const numericFormatter = new Intl.DateTimeFormat('en-u-ca-' + calendar + '-nu-latn', {
|
|
11
|
+
year: 'numeric',
|
|
12
|
+
month: 'numeric',
|
|
13
|
+
day: 'numeric',
|
|
14
|
+
});
|
|
15
|
+
const eraFormatter = new Intl.DateTimeFormat('en-u-ca-' + calendar, {
|
|
16
|
+
era: 'short',
|
|
17
|
+
});
|
|
18
|
+
const numericParts = numericFormatter.formatToParts(date);
|
|
19
|
+
const eraParts = eraFormatter.formatToParts(date);
|
|
20
|
+
const result = {
|
|
21
|
+
year: 0,
|
|
22
|
+
month: 0,
|
|
23
|
+
day: 0,
|
|
24
|
+
calendar,
|
|
25
|
+
};
|
|
26
|
+
for (const part of numericParts) {
|
|
27
|
+
switch (part.type) {
|
|
28
|
+
case 'year': {
|
|
29
|
+
// Parse year, handling relatedYear for Chinese calendar
|
|
30
|
+
const parsed = parseInt(part.value, 10);
|
|
31
|
+
result.year = isNaN(parsed) ? 0 : parsed;
|
|
32
|
+
break;
|
|
33
|
+
}
|
|
34
|
+
case 'month': {
|
|
35
|
+
// Some calendars return month names instead of numbers
|
|
36
|
+
const parsed = parseInt(part.value, 10);
|
|
37
|
+
if (!isNaN(parsed)) {
|
|
38
|
+
result.month = parsed;
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
// For calendars that return month names, get month index
|
|
42
|
+
result.month = getMonthIndexFromName(part.value, calendar) || 0;
|
|
43
|
+
}
|
|
44
|
+
break;
|
|
45
|
+
}
|
|
46
|
+
case 'day':
|
|
47
|
+
result.day = parseInt(part.value, 10);
|
|
48
|
+
break;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
// Get era separately
|
|
52
|
+
for (const part of eraParts) {
|
|
53
|
+
if (part.type === 'era') {
|
|
54
|
+
result.era = part.value;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
// Special handling for Chinese calendar year (uses related gregorian year)
|
|
58
|
+
if (calendar === 'chinese' && result.year === 0) {
|
|
59
|
+
const relatedFormatter = new Intl.DateTimeFormat('en-u-ca-chinese', {
|
|
60
|
+
year: 'numeric',
|
|
61
|
+
});
|
|
62
|
+
const relatedParts = relatedFormatter.formatToParts(date);
|
|
63
|
+
for (const part of relatedParts) {
|
|
64
|
+
if (part.type === 'relatedYear') {
|
|
65
|
+
result.year = parseInt(part.value, 10);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return result;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Maps month name to month number for non-numeric month calendars
|
|
73
|
+
*/
|
|
74
|
+
function getMonthIndexFromName(monthName, calendar) {
|
|
75
|
+
const hebrewMonths = {
|
|
76
|
+
'Tishri': 1, 'Tishrei': 1,
|
|
77
|
+
'Heshvan': 2, 'Cheshvan': 2, 'Marcheshvan': 2,
|
|
78
|
+
'Kislev': 3,
|
|
79
|
+
'Tevet': 4, 'Teves': 4,
|
|
80
|
+
'Shevat': 5, 'Shvat': 5,
|
|
81
|
+
'Adar': 6, 'Adar I': 6,
|
|
82
|
+
'Adar II': 7, 'Adar Sheni': 7,
|
|
83
|
+
'Nisan': 8, 'Nissan': 8,
|
|
84
|
+
'Iyar': 9, 'Iyyar': 9,
|
|
85
|
+
'Sivan': 10, 'Siwan': 10,
|
|
86
|
+
'Tammuz': 11, 'Tamuz': 11,
|
|
87
|
+
'Av': 12, 'Menachem Av': 12,
|
|
88
|
+
'Elul': 13,
|
|
89
|
+
};
|
|
90
|
+
if (calendar === 'hebrew') {
|
|
91
|
+
return hebrewMonths[monthName] || 0;
|
|
92
|
+
}
|
|
93
|
+
return 0;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Convert Gregorian date to Hebrew calendar
|
|
97
|
+
* @example toHebrewDate(new Date('2024-03-25')) // { year: 5784, month: 6, day: 15, calendar: 'hebrew' }
|
|
98
|
+
*/
|
|
99
|
+
export function toHebrewDate(date) {
|
|
100
|
+
return extractCalendarParts(date, 'hebrew');
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Convert Gregorian date to Islamic calendar (default: islamic-umalqura)
|
|
104
|
+
* @param date - Date to convert
|
|
105
|
+
* @param variant - Islamic calendar variant: 'islamic', 'islamic-umalqura', 'islamic-civil'
|
|
106
|
+
* @example toIslamicDate(new Date('2024-03-25')) // { year: 1445, month: 9, day: 15, calendar: 'islamic-umalqura' }
|
|
107
|
+
*/
|
|
108
|
+
export function toIslamicDate(date, variant = 'islamic-umalqura') {
|
|
109
|
+
return extractCalendarParts(date, variant);
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Convert Gregorian date to Buddhist calendar (Thai Solar)
|
|
113
|
+
* Buddhist Era = Gregorian Year + 543
|
|
114
|
+
* @example toBuddhistDate(new Date('2024-03-25')) // { year: 2567, month: 3, day: 25, calendar: 'buddhist' }
|
|
115
|
+
*/
|
|
116
|
+
export function toBuddhistDate(date) {
|
|
117
|
+
return extractCalendarParts(date, 'buddhist');
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Convert Gregorian date to Japanese calendar with era
|
|
121
|
+
* @example toJapaneseDate(new Date('2024-03-25')) // { year: 6, month: 3, day: 25, era: 'Reiwa', calendar: 'japanese' }
|
|
122
|
+
*/
|
|
123
|
+
export function toJapaneseDate(date) {
|
|
124
|
+
const result = extractCalendarParts(date, 'japanese');
|
|
125
|
+
// Get full era name
|
|
126
|
+
const eraFormatter = new Intl.DateTimeFormat('en-u-ca-japanese', { era: 'long' });
|
|
127
|
+
const eraParts = eraFormatter.formatToParts(date);
|
|
128
|
+
const eraPart = eraParts.find(p => p.type === 'era');
|
|
129
|
+
if (eraPart) {
|
|
130
|
+
result.era = eraPart.value;
|
|
131
|
+
}
|
|
132
|
+
return result;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Convert Gregorian date to Persian (Jalali/Solar Hijri) calendar
|
|
136
|
+
* @example toPersianDate(new Date('2024-03-20')) // { year: 1403, month: 1, day: 1, calendar: 'persian' }
|
|
137
|
+
*/
|
|
138
|
+
export function toPersianDate(date) {
|
|
139
|
+
return extractCalendarParts(date, 'persian');
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Convert Gregorian date to Chinese lunar calendar
|
|
143
|
+
* @example toChineseDate(new Date('2024-02-10')) // { year: 4721, month: 1, day: 1, calendar: 'chinese' }
|
|
144
|
+
*/
|
|
145
|
+
export function toChineseDate(date) {
|
|
146
|
+
return extractCalendarParts(date, 'chinese');
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Format date in specified calendar system
|
|
150
|
+
* @param date - Date to format
|
|
151
|
+
* @param calendar - Calendar system to use
|
|
152
|
+
* @param locale - Locale for formatting (default: 'en')
|
|
153
|
+
* @param options - Additional Intl.DateTimeFormat options
|
|
154
|
+
*/
|
|
155
|
+
export function formatInCalendar(date, calendar, locale = 'en', options = {}) {
|
|
156
|
+
const localeWithCalendar = `${locale}-u-ca-${calendar}`;
|
|
157
|
+
const formatter = new Intl.DateTimeFormat(localeWithCalendar, {
|
|
158
|
+
dateStyle: 'long',
|
|
159
|
+
...options,
|
|
160
|
+
});
|
|
161
|
+
return formatter.format(date);
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Get month names for a specific calendar system
|
|
165
|
+
* @param calendar - Calendar system
|
|
166
|
+
* @param locale - Locale for month names (default: 'en')
|
|
167
|
+
* @param format - Month name format: 'long', 'short', 'narrow'
|
|
168
|
+
*/
|
|
169
|
+
export function getCalendarMonthNames(calendar, locale = 'en', format = 'long') {
|
|
170
|
+
const localeWithCalendar = `${locale}-u-ca-${calendar}`;
|
|
171
|
+
const formatter = new Intl.DateTimeFormat(localeWithCalendar, { month: format });
|
|
172
|
+
// Generate month names by iterating through a reference year
|
|
173
|
+
const months = [];
|
|
174
|
+
const year = 2024;
|
|
175
|
+
// Most calendars have 12 months, Hebrew has 13 in leap years
|
|
176
|
+
const maxMonths = calendar === 'hebrew' ? 13 : 12;
|
|
177
|
+
for (let month = 0; month < maxMonths; month++) {
|
|
178
|
+
try {
|
|
179
|
+
const date = new Date(year, month, 15);
|
|
180
|
+
months.push(formatter.format(date));
|
|
181
|
+
}
|
|
182
|
+
catch {
|
|
183
|
+
break;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
return months;
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Get era name for Japanese calendar
|
|
190
|
+
* @param date - Date to get era for
|
|
191
|
+
* @param format - Era format: 'long', 'short', 'narrow'
|
|
192
|
+
*/
|
|
193
|
+
export function getJapaneseEra(date, format = 'long') {
|
|
194
|
+
const formatter = new Intl.DateTimeFormat('en-u-ca-japanese', { era: format });
|
|
195
|
+
const parts = formatter.formatToParts(date);
|
|
196
|
+
const eraPart = parts.find(p => p.type === 'era');
|
|
197
|
+
return eraPart?.value || '';
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Get all Japanese era names with their start dates
|
|
201
|
+
*/
|
|
202
|
+
export function getJapaneseEras() {
|
|
203
|
+
return [
|
|
204
|
+
{ name: 'Meiji', start: new Date(1868, 9, 23) },
|
|
205
|
+
{ name: 'Taisho', start: new Date(1912, 6, 30) },
|
|
206
|
+
{ name: 'Showa', start: new Date(1926, 11, 25) },
|
|
207
|
+
{ name: 'Heisei', start: new Date(1989, 0, 8) },
|
|
208
|
+
{ name: 'Reiwa', start: new Date(2019, 4, 1) },
|
|
209
|
+
];
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Check if a Hebrew year is a leap year (has 13 months)
|
|
213
|
+
* @param hebrewYear - Year in Hebrew calendar
|
|
214
|
+
*/
|
|
215
|
+
export function isHebrewLeapYear(hebrewYear) {
|
|
216
|
+
// Hebrew leap years follow a 19-year cycle
|
|
217
|
+
// Years 3, 6, 8, 11, 14, 17, 19 are leap years
|
|
218
|
+
const position = hebrewYear % 19;
|
|
219
|
+
return [3, 6, 8, 11, 14, 17, 0].includes(position); // 0 = 19th year
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Get Hebrew month name
|
|
223
|
+
* @param month - Month number (1-13)
|
|
224
|
+
* @param isLeapYear - Whether the year is a leap year
|
|
225
|
+
*/
|
|
226
|
+
export function getHebrewMonthName(month, isLeapYear = false) {
|
|
227
|
+
const months = [
|
|
228
|
+
'Nisan', 'Iyar', 'Sivan', 'Tammuz', 'Av', 'Elul',
|
|
229
|
+
'Tishrei', 'Cheshvan', 'Kislev', 'Tevet', 'Shevat',
|
|
230
|
+
isLeapYear ? 'Adar I' : 'Adar',
|
|
231
|
+
'Adar II'
|
|
232
|
+
];
|
|
233
|
+
return months[month - 1] || '';
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Get Islamic month name
|
|
237
|
+
* @param month - Month number (1-12)
|
|
238
|
+
*/
|
|
239
|
+
export function getIslamicMonthName(month) {
|
|
240
|
+
const months = [
|
|
241
|
+
'Muharram', 'Safar', 'Rabi\' al-Awwal', 'Rabi\' al-Thani',
|
|
242
|
+
'Jumada al-Awwal', 'Jumada al-Thani', 'Rajab', 'Sha\'ban',
|
|
243
|
+
'Ramadan', 'Shawwal', 'Dhu al-Qi\'dah', 'Dhu al-Hijjah'
|
|
244
|
+
];
|
|
245
|
+
return months[month - 1] || '';
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Get Persian month name
|
|
249
|
+
* @param month - Month number (1-12)
|
|
250
|
+
*/
|
|
251
|
+
export function getPersianMonthName(month) {
|
|
252
|
+
const months = [
|
|
253
|
+
'Farvardin', 'Ordibehesht', 'Khordad', 'Tir', 'Mordad', 'Shahrivar',
|
|
254
|
+
'Mehr', 'Aban', 'Azar', 'Dey', 'Bahman', 'Esfand'
|
|
255
|
+
];
|
|
256
|
+
return months[month - 1] || '';
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* Check if a Persian year is a leap year
|
|
260
|
+
* Uses the 2820-year cycle algorithm
|
|
261
|
+
*/
|
|
262
|
+
export function isPersianLeapYear(persianYear) {
|
|
263
|
+
// Simplified 33-year cycle approximation
|
|
264
|
+
const remainder = persianYear % 33;
|
|
265
|
+
return [1, 5, 9, 13, 17, 22, 26, 30].includes(remainder);
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* Get Chinese zodiac animal for a year
|
|
269
|
+
* @param gregorianYear - Gregorian year
|
|
270
|
+
*/
|
|
271
|
+
export function getChineseZodiac(gregorianYear) {
|
|
272
|
+
const animals = [
|
|
273
|
+
'Rat', 'Ox', 'Tiger', 'Rabbit', 'Dragon', 'Snake',
|
|
274
|
+
'Horse', 'Goat', 'Monkey', 'Rooster', 'Dog', 'Pig'
|
|
275
|
+
];
|
|
276
|
+
// 1900 was Year of the Rat
|
|
277
|
+
const index = (gregorianYear - 1900) % 12;
|
|
278
|
+
return animals[index >= 0 ? index : index + 12];
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* Get Chinese element for a year
|
|
282
|
+
* @param gregorianYear - Gregorian year
|
|
283
|
+
*/
|
|
284
|
+
export function getChineseElement(gregorianYear) {
|
|
285
|
+
const elements = ['Metal', 'Water', 'Wood', 'Fire', 'Earth'];
|
|
286
|
+
// Each element covers 2 years
|
|
287
|
+
const index = Math.floor(((gregorianYear - 1900) % 10) / 2);
|
|
288
|
+
return elements[index >= 0 ? index : index + 5];
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* Get full Chinese zodiac description (element + animal)
|
|
292
|
+
* @param gregorianYear - Gregorian year
|
|
293
|
+
*/
|
|
294
|
+
export function getChineseZodiacFull(gregorianYear) {
|
|
295
|
+
return `${getChineseElement(gregorianYear)} ${getChineseZodiac(gregorianYear)}`;
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* Convert calendar date to string representation
|
|
299
|
+
*/
|
|
300
|
+
export function calendarDateToString(calendarDate) {
|
|
301
|
+
const { year, month, day, era, calendar } = calendarDate;
|
|
302
|
+
const eraStr = era ? ` ${era}` : '';
|
|
303
|
+
return `${year}/${month}/${day}${eraStr} (${calendar})`;
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* Compare two calendar dates
|
|
307
|
+
* @returns negative if a < b, 0 if equal, positive if a > b
|
|
308
|
+
*/
|
|
309
|
+
export function compareCalendarDates(a, b) {
|
|
310
|
+
if (a.calendar !== b.calendar) {
|
|
311
|
+
throw new Error('Cannot compare dates from different calendar systems');
|
|
312
|
+
}
|
|
313
|
+
if (a.year !== b.year)
|
|
314
|
+
return a.year - b.year;
|
|
315
|
+
if (a.month !== b.month)
|
|
316
|
+
return a.month - b.month;
|
|
317
|
+
return a.day - b.day;
|
|
318
|
+
}
|
|
319
|
+
/**
|
|
320
|
+
* Get current date in specified calendar
|
|
321
|
+
*/
|
|
322
|
+
export function today(calendar) {
|
|
323
|
+
return extractCalendarParts(new Date(), calendar);
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* Check if two calendar dates are the same day
|
|
327
|
+
*/
|
|
328
|
+
export function isSameCalendarDay(a, b) {
|
|
329
|
+
return a.calendar === b.calendar &&
|
|
330
|
+
a.year === b.year &&
|
|
331
|
+
a.month === b.month &&
|
|
332
|
+
a.day === b.day;
|
|
333
|
+
}
|
|
334
|
+
/**
|
|
335
|
+
* Get supported calendar systems
|
|
336
|
+
*/
|
|
337
|
+
export function getSupportedCalendars() {
|
|
338
|
+
return [
|
|
339
|
+
'hebrew',
|
|
340
|
+
'islamic',
|
|
341
|
+
'islamic-umalqura',
|
|
342
|
+
'islamic-civil',
|
|
343
|
+
'buddhist',
|
|
344
|
+
'japanese',
|
|
345
|
+
'persian',
|
|
346
|
+
'chinese'
|
|
347
|
+
];
|
|
348
|
+
}
|
package/dist/compare.d.ts
CHANGED
|
@@ -214,4 +214,31 @@ export declare function partitionDates(dates: Date[], predicate: (date: Date) =>
|
|
|
214
214
|
* nthDate(dates, 2) // third earliest
|
|
215
215
|
*/
|
|
216
216
|
export declare function nthDate(dates: Date[], n: number): Date | undefined;
|
|
217
|
+
/**
|
|
218
|
+
* Find the index of the closest date to a target in an array
|
|
219
|
+
* @param dates - Array of candidate dates
|
|
220
|
+
* @param target - The target date
|
|
221
|
+
* @returns Index of the closest date, or -1 if array is empty
|
|
222
|
+
* @example
|
|
223
|
+
* closestIndexTo([date1, date2, date3], targetDate) // 1
|
|
224
|
+
*/
|
|
225
|
+
export declare function closestIndexTo(dates: Date[], target: Date): number;
|
|
226
|
+
/**
|
|
227
|
+
* Get the number of overlapping days between two date ranges
|
|
228
|
+
* @param range1 - First range { start, end }
|
|
229
|
+
* @param range2 - Second range { start, end }
|
|
230
|
+
* @returns Number of overlapping days (0 if no overlap)
|
|
231
|
+
* @example
|
|
232
|
+
* getOverlappingDaysInIntervals(
|
|
233
|
+
* { start: new Date('2024-01-01'), end: new Date('2024-01-10') },
|
|
234
|
+
* { start: new Date('2024-01-05'), end: new Date('2024-01-15') }
|
|
235
|
+
* ) // 6 (Jan 5-10 inclusive)
|
|
236
|
+
*/
|
|
237
|
+
export declare function getOverlappingDaysInIntervals(range1: {
|
|
238
|
+
start: Date;
|
|
239
|
+
end: Date;
|
|
240
|
+
}, range2: {
|
|
241
|
+
start: Date;
|
|
242
|
+
end: Date;
|
|
243
|
+
}): number;
|
|
217
244
|
//# sourceMappingURL=compare.d.ts.map
|
package/dist/compare.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"compare.d.ts","sourceRoot":"","sources":["../src/compare.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH;;;;;;;GAOG;AACH,wBAAgB,YAAY,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,IAAI,GAAG,MAAM,CAErD;AAED;;;;;;;GAOG;AACH,wBAAgB,gBAAgB,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,IAAI,GAAG,MAAM,CAEzD;AAED;;;;;;;;GAQG;AACH,wBAAgB,SAAS,CACvB,KAAK,EAAE,IAAI,EAAE,EACb,SAAS,GAAE,KAAK,GAAG,MAAc,GAChC,IAAI,EAAE,CAIR;AAED;;;;;;GAMG;AACH,wBAAgB,OAAO,CAAC,KAAK,EAAE,IAAI,EAAE,GAAG,IAAI,GAAG,SAAS,CAGvD;AAED;;;;;;GAMG;AACH,wBAAgB,OAAO,CAAC,KAAK,EAAE,IAAI,EAAE,GAAG,IAAI,GAAG,SAAS,CAGvD;AAED;;;;;;GAMG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,IAAI,EAAE,GAAG;IAAE,GAAG,EAAE,IAAI,CAAC;IAAC,GAAG,EAAE,IAAI,CAAA;CAAE,GA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G,EAAE,IAAI,CAAA;CAAE,EAClC,MAAM,EAAE;IAAE,KAAK,EAAE,IAAI,CAAC;IAAC,GAAG,EAAE,IAAI,CAAA;CAAE,GACjC,MAAM,CAaR"}
|
package/dist/compare.js
CHANGED
|
@@ -415,3 +415,49 @@ export function nthDate(dates, n) {
|
|
|
415
415
|
return undefined;
|
|
416
416
|
return sorted[index];
|
|
417
417
|
}
|
|
418
|
+
/**
|
|
419
|
+
* Find the index of the closest date to a target in an array
|
|
420
|
+
* @param dates - Array of candidate dates
|
|
421
|
+
* @param target - The target date
|
|
422
|
+
* @returns Index of the closest date, or -1 if array is empty
|
|
423
|
+
* @example
|
|
424
|
+
* closestIndexTo([date1, date2, date3], targetDate) // 1
|
|
425
|
+
*/
|
|
426
|
+
export function closestIndexTo(dates, target) {
|
|
427
|
+
if (dates.length === 0)
|
|
428
|
+
return -1;
|
|
429
|
+
const targetTime = target.getTime();
|
|
430
|
+
let closestIndex = 0;
|
|
431
|
+
let minDiff = Math.abs(dates[0].getTime() - targetTime);
|
|
432
|
+
for (let i = 1; i < dates.length; i++) {
|
|
433
|
+
const diff = Math.abs(dates[i].getTime() - targetTime);
|
|
434
|
+
if (diff < minDiff) {
|
|
435
|
+
minDiff = diff;
|
|
436
|
+
closestIndex = i;
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
return closestIndex;
|
|
440
|
+
}
|
|
441
|
+
/**
|
|
442
|
+
* Get the number of overlapping days between two date ranges
|
|
443
|
+
* @param range1 - First range { start, end }
|
|
444
|
+
* @param range2 - Second range { start, end }
|
|
445
|
+
* @returns Number of overlapping days (0 if no overlap)
|
|
446
|
+
* @example
|
|
447
|
+
* getOverlappingDaysInIntervals(
|
|
448
|
+
* { start: new Date('2024-01-01'), end: new Date('2024-01-10') },
|
|
449
|
+
* { start: new Date('2024-01-05'), end: new Date('2024-01-15') }
|
|
450
|
+
* ) // 6 (Jan 5-10 inclusive)
|
|
451
|
+
*/
|
|
452
|
+
export function getOverlappingDaysInIntervals(range1, range2) {
|
|
453
|
+
const start1 = new Date(range1.start.getFullYear(), range1.start.getMonth(), range1.start.getDate());
|
|
454
|
+
const end1 = new Date(range1.end.getFullYear(), range1.end.getMonth(), range1.end.getDate());
|
|
455
|
+
const start2 = new Date(range2.start.getFullYear(), range2.start.getMonth(), range2.start.getDate());
|
|
456
|
+
const end2 = new Date(range2.end.getFullYear(), range2.end.getMonth(), range2.end.getDate());
|
|
457
|
+
const overlapStart = start1 > start2 ? start1 : start2;
|
|
458
|
+
const overlapEnd = end1 < end2 ? end1 : end2;
|
|
459
|
+
if (overlapStart > overlapEnd)
|
|
460
|
+
return 0;
|
|
461
|
+
const diffMs = overlapEnd.getTime() - overlapStart.getTime();
|
|
462
|
+
return Math.floor(diffMs / 86400000) + 1;
|
|
463
|
+
}
|
package/dist/esm/calculate.d.ts
CHANGED
|
@@ -51,4 +51,29 @@ export declare function isBetween(date: Date, start: Date, end: Date, inclusive?
|
|
|
51
51
|
* @param endDate - end date
|
|
52
52
|
*/
|
|
53
53
|
export declare function businessDaysBetween(startDate: Date, endDate: Date): number;
|
|
54
|
+
/**
|
|
55
|
+
* Round a date to the nearest unit (rounds to closest)
|
|
56
|
+
* @param date - date to round
|
|
57
|
+
* @param unit - unit to round to
|
|
58
|
+
* @example
|
|
59
|
+
* roundToNearestUnit(new Date('2024-03-15T14:37:00'), 'hour') // 15:00
|
|
60
|
+
* roundToNearestUnit(new Date('2024-03-15T14:22:00'), 'hour') // 14:00
|
|
61
|
+
*/
|
|
62
|
+
export declare function roundToNearestUnit(date: Date, unit: 'second' | 'minute' | 'hour' | 'day' | 'week' | 'month'): Date;
|
|
63
|
+
/**
|
|
64
|
+
* Round a date up (ceiling) to the specified unit
|
|
65
|
+
* @param date - date to round
|
|
66
|
+
* @param unit - unit to round to
|
|
67
|
+
* @example
|
|
68
|
+
* ceilDate(new Date('2024-03-15T14:01:00'), 'hour') // 15:00
|
|
69
|
+
*/
|
|
70
|
+
export declare function ceilDate(date: Date, unit: 'second' | 'minute' | 'hour' | 'day' | 'week' | 'month'): Date;
|
|
71
|
+
/**
|
|
72
|
+
* Round a date down (floor) to the specified unit
|
|
73
|
+
* @param date - date to round
|
|
74
|
+
* @param unit - unit to round to
|
|
75
|
+
* @example
|
|
76
|
+
* floorDate(new Date('2024-03-15T14:59:00'), 'hour') // 14:00
|
|
77
|
+
*/
|
|
78
|
+
export declare function floorDate(date: Date, unit: 'second' | 'minute' | 'hour' | 'day' | 'week' | 'month'): Date;
|
|
54
79
|
//# sourceMappingURL=calculate.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"calculate.d.ts","sourceRoot":"","sources":["../../src/calculate.ts"],"names":[],"mappings":"AAAA,OAAO,EAQL,QAAQ,EACT,MAAM,gBAAgB,CAAC;AAExB;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAC/B,KAAK,EAAE,IAAI,EACX,KAAK,EAAE,IAAI,EACX,IAAI,GAAE,QAAyB,EAC/B,OAAO,GAAE,OAAc,GACtB,MAAM,CAkCR;AAED;;;;;GAKG;AACH,wBAAgB,OAAO,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,QAAQ,GAAG,IAAI,CAgDxE;AAED;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,QAAQ,GAAG,IAAI,CAE7E;AAED;;;;GAIG;AACH,wBAAgB,OAAO,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,GAAG,MAAM,GAAG,OAAO,GAAG,MAAM,GAAG,MAAM,GAAG,QAAQ,GAAG,IAAI,CA4BrG;AAED;;;;GAIG;AACH,wBAAgB,KAAK,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,GAAG,MAAM,GAAG,OAAO,GAAG,MAAM,GAAG,MAAM,GAAG,QAAQ,GAAG,IAAI,CA4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AG,EAAE,IAAI,EAAE,SAAS,GAAE,OAAc,GAAG,OAAO,CAShG;AAED;;;;GAIG;AACH,wBAAgB,mBAAmB,CAAC,SAAS,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,GAAG,MAAM,CAa1E;AAED;;;;;;;GAOG;AACH,wBAAgB,kBAAkB,CAChC,IAAI,EAAE,IAAI,EACV,IAAI,EAAE,QAAQ,GAAG,QAAQ,GAAG,MAAM,GAAG,KAAK,GAAG,MAAM,GAAG,OAAO,GAC5D,IAAI,CA2CN;AAED;;;;;;GAMG;AACH,wBAAgB,QAAQ,CACtB,IAAI,EAAE,IAAI,EACV,IAAI,EAAE,QAAQ,GAAG,QAAQ,GAAG,MAAM,GAAG,KAAK,GAAG,MAAM,GAAG,OAAO,GAC5D,IAAI,CA0BN;AAED;;;;;;GAMG;AACH,wBAAgB,SAAS,CACvB,IAAI,EAAE,IAAI,EACV,IAAI,EAAE,QAAQ,GAAG,QAAQ,GAAG,MAAM,GAAG,KAAK,GAAG,MAAM,GAAG,OAAO,GAC5D,IAAI,CA8BN"}
|
package/dist/esm/calculate.js
CHANGED
|
@@ -201,3 +201,128 @@ export function businessDaysBetween(startDate, endDate) {
|
|
|
201
201
|
}
|
|
202
202
|
return count;
|
|
203
203
|
}
|
|
204
|
+
/**
|
|
205
|
+
* Round a date to the nearest unit (rounds to closest)
|
|
206
|
+
* @param date - date to round
|
|
207
|
+
* @param unit - unit to round to
|
|
208
|
+
* @example
|
|
209
|
+
* roundToNearestUnit(new Date('2024-03-15T14:37:00'), 'hour') // 15:00
|
|
210
|
+
* roundToNearestUnit(new Date('2024-03-15T14:22:00'), 'hour') // 14:00
|
|
211
|
+
*/
|
|
212
|
+
export function roundToNearestUnit(date, unit) {
|
|
213
|
+
const d = new Date(date);
|
|
214
|
+
switch (unit) {
|
|
215
|
+
case 'second':
|
|
216
|
+
if (d.getMilliseconds() >= 500)
|
|
217
|
+
d.setSeconds(d.getSeconds() + 1);
|
|
218
|
+
d.setMilliseconds(0);
|
|
219
|
+
break;
|
|
220
|
+
case 'minute':
|
|
221
|
+
if (d.getSeconds() >= 30)
|
|
222
|
+
d.setMinutes(d.getMinutes() + 1);
|
|
223
|
+
d.setSeconds(0, 0);
|
|
224
|
+
break;
|
|
225
|
+
case 'hour':
|
|
226
|
+
if (d.getMinutes() >= 30)
|
|
227
|
+
d.setHours(d.getHours() + 1);
|
|
228
|
+
d.setMinutes(0, 0, 0);
|
|
229
|
+
break;
|
|
230
|
+
case 'day':
|
|
231
|
+
if (d.getHours() >= 12)
|
|
232
|
+
d.setDate(d.getDate() + 1);
|
|
233
|
+
d.setHours(0, 0, 0, 0);
|
|
234
|
+
break;
|
|
235
|
+
case 'week': {
|
|
236
|
+
const dayOfWeek = d.getDay();
|
|
237
|
+
const daysFromMonday = dayOfWeek === 0 ? 6 : dayOfWeek - 1;
|
|
238
|
+
if (daysFromMonday >= 4) {
|
|
239
|
+
d.setDate(d.getDate() + (7 - daysFromMonday));
|
|
240
|
+
}
|
|
241
|
+
else {
|
|
242
|
+
d.setDate(d.getDate() - daysFromMonday);
|
|
243
|
+
}
|
|
244
|
+
d.setHours(0, 0, 0, 0);
|
|
245
|
+
break;
|
|
246
|
+
}
|
|
247
|
+
case 'month': {
|
|
248
|
+
const daysInMonth = new Date(d.getFullYear(), d.getMonth() + 1, 0).getDate();
|
|
249
|
+
if (d.getDate() > daysInMonth / 2) {
|
|
250
|
+
d.setMonth(d.getMonth() + 1);
|
|
251
|
+
}
|
|
252
|
+
d.setDate(1);
|
|
253
|
+
d.setHours(0, 0, 0, 0);
|
|
254
|
+
break;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
return d;
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Round a date up (ceiling) to the specified unit
|
|
261
|
+
* @param date - date to round
|
|
262
|
+
* @param unit - unit to round to
|
|
263
|
+
* @example
|
|
264
|
+
* ceilDate(new Date('2024-03-15T14:01:00'), 'hour') // 15:00
|
|
265
|
+
*/
|
|
266
|
+
export function ceilDate(date, unit) {
|
|
267
|
+
const floored = floorDate(date, unit);
|
|
268
|
+
if (floored.getTime() === date.getTime())
|
|
269
|
+
return new Date(date);
|
|
270
|
+
const d = new Date(floored);
|
|
271
|
+
switch (unit) {
|
|
272
|
+
case 'second':
|
|
273
|
+
d.setSeconds(d.getSeconds() + 1);
|
|
274
|
+
break;
|
|
275
|
+
case 'minute':
|
|
276
|
+
d.setMinutes(d.getMinutes() + 1);
|
|
277
|
+
break;
|
|
278
|
+
case 'hour':
|
|
279
|
+
d.setHours(d.getHours() + 1);
|
|
280
|
+
break;
|
|
281
|
+
case 'day':
|
|
282
|
+
d.setDate(d.getDate() + 1);
|
|
283
|
+
break;
|
|
284
|
+
case 'week':
|
|
285
|
+
d.setDate(d.getDate() + 7);
|
|
286
|
+
break;
|
|
287
|
+
case 'month':
|
|
288
|
+
d.setMonth(d.getMonth() + 1);
|
|
289
|
+
break;
|
|
290
|
+
}
|
|
291
|
+
return d;
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* Round a date down (floor) to the specified unit
|
|
295
|
+
* @param date - date to round
|
|
296
|
+
* @param unit - unit to round to
|
|
297
|
+
* @example
|
|
298
|
+
* floorDate(new Date('2024-03-15T14:59:00'), 'hour') // 14:00
|
|
299
|
+
*/
|
|
300
|
+
export function floorDate(date, unit) {
|
|
301
|
+
const d = new Date(date);
|
|
302
|
+
switch (unit) {
|
|
303
|
+
case 'second':
|
|
304
|
+
d.setMilliseconds(0);
|
|
305
|
+
break;
|
|
306
|
+
case 'minute':
|
|
307
|
+
d.setSeconds(0, 0);
|
|
308
|
+
break;
|
|
309
|
+
case 'hour':
|
|
310
|
+
d.setMinutes(0, 0, 0);
|
|
311
|
+
break;
|
|
312
|
+
case 'day':
|
|
313
|
+
d.setHours(0, 0, 0, 0);
|
|
314
|
+
break;
|
|
315
|
+
case 'week': {
|
|
316
|
+
const dayOfWeek = d.getDay();
|
|
317
|
+
const daysFromMonday = dayOfWeek === 0 ? 6 : dayOfWeek - 1;
|
|
318
|
+
d.setDate(d.getDate() - daysFromMonday);
|
|
319
|
+
d.setHours(0, 0, 0, 0);
|
|
320
|
+
break;
|
|
321
|
+
}
|
|
322
|
+
case 'month':
|
|
323
|
+
d.setDate(1);
|
|
324
|
+
d.setHours(0, 0, 0, 0);
|
|
325
|
+
break;
|
|
326
|
+
}
|
|
327
|
+
return d;
|
|
328
|
+
}
|
package/dist/esm/calendar.d.ts
CHANGED
|
@@ -182,4 +182,49 @@ export declare function getEndOfWeek(date: Date): Date;
|
|
|
182
182
|
* @param month - month (0-11)
|
|
183
183
|
*/
|
|
184
184
|
export declare function getWeeksInMonth(year: number, month: number): Date[][];
|
|
185
|
+
/**
|
|
186
|
+
* Get the ISO week-numbering year (year the week belongs to)
|
|
187
|
+
* @param date - date to get week year for
|
|
188
|
+
* @returns The year the ISO week belongs to (may differ from calendar year)
|
|
189
|
+
* @example
|
|
190
|
+
* getWeekYear(new Date('2024-01-01')) // 2024
|
|
191
|
+
* getWeekYear(new Date('2020-12-31')) // 2020 (belongs to week 53 of 2020)
|
|
192
|
+
*/
|
|
193
|
+
export declare function getWeekYear(date: Date): number;
|
|
194
|
+
/**
|
|
195
|
+
* Get the last day of the decade containing the date
|
|
196
|
+
* @param date - any date
|
|
197
|
+
* @returns December 31 of the last year of the decade
|
|
198
|
+
* @example
|
|
199
|
+
* lastDayOfDecade(new Date('2024-06-15')) // 2029-12-31
|
|
200
|
+
*/
|
|
201
|
+
export declare function lastDayOfDecade(date: Date): Date;
|
|
202
|
+
/**
|
|
203
|
+
* Get the first day of the decade containing the date
|
|
204
|
+
* @param date - any date
|
|
205
|
+
* @returns January 1 of the first year of the decade
|
|
206
|
+
* @example
|
|
207
|
+
* firstDayOfDecade(new Date('2024-06-15')) // 2020-01-01
|
|
208
|
+
*/
|
|
209
|
+
export declare function firstDayOfDecade(date: Date): Date;
|
|
210
|
+
/**
|
|
211
|
+
* Get the last day of the century containing the date
|
|
212
|
+
* @param date - any date
|
|
213
|
+
* @returns December 31 of the last year of the century
|
|
214
|
+
* @example
|
|
215
|
+
* lastDayOfCentury(new Date('2024-06-15')) // 2099-12-31
|
|
216
|
+
*/
|
|
217
|
+
export declare function lastDayOfCentury(date: Date): Date;
|
|
218
|
+
/**
|
|
219
|
+
* Get the start of a quarter
|
|
220
|
+
* @param date - any date
|
|
221
|
+
* @returns First day of the quarter
|
|
222
|
+
*/
|
|
223
|
+
export declare function getStartOfQuarter(date: Date): Date;
|
|
224
|
+
/**
|
|
225
|
+
* Get the end of a quarter
|
|
226
|
+
* @param date - any date
|
|
227
|
+
* @returns Last day of the quarter
|
|
228
|
+
*/
|
|
229
|
+
export declare function getEndOfQuarter(date: Date): Date;
|
|
185
230
|
//# sourceMappingURL=calendar.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"calendar.d.ts","sourceRoot":"","sources":["../../src/calendar.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH;;;GAGG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,IAAI,GAAG,MAAM,CAMhD;AAED;;;GAGG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,IAAI,GAAG,MAAM,CAKjD;AAED;;;GAGG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,IAAI,GAAG,MAAM,CAE7C;AAED;;;GAGG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,IAAI,GAAG,MAAM,CAI/C;AAED;;;GAGG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAYnD;AAED;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAElE;AAED;;;GAGG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAElD;AAUD;;;GAGG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,IA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