ts-time-utils 4.1.0 → 4.4.0
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 +81 -31
- package/dist/{age.js → age.cjs} +14 -6
- package/dist/{calculate.js → calculate.cjs} +30 -18
- package/dist/{calendar.js → calendar.cjs} +80 -39
- package/dist/{calendars.js → calendars.cjs} +48 -23
- package/dist/{chain.js → chain.cjs} +41 -40
- package/dist/{compare.js → compare.cjs} +58 -28
- package/dist/constants.cjs +19 -0
- package/dist/{countdown.js → countdown.cjs} +16 -7
- package/dist/{cron.js → cron.cjs} +20 -9
- package/dist/{dateRange.js → dateRange.cjs} +42 -26
- package/dist/{duration.js → duration.cjs} +56 -44
- package/dist/esm/chain.js +0 -5
- package/dist/esm/naturalLanguage.d.ts +1 -3
- package/dist/esm/naturalLanguage.d.ts.map +1 -1
- package/dist/esm/naturalLanguage.js +9 -2
- package/dist/esm/plugins.d.ts +0 -6
- package/dist/esm/plugins.d.ts.map +1 -1
- package/dist/esm/plugins.js +36 -42
- package/dist/esm/recurrence.d.ts.map +1 -1
- package/dist/esm/recurrence.js +3 -5
- package/dist/esm/timezone.d.ts +6 -1
- package/dist/esm/timezone.d.ts.map +1 -1
- package/dist/esm/timezone.js +106 -66
- package/dist/esm/types.d.ts +0 -4
- package/dist/esm/types.d.ts.map +1 -1
- package/dist/{finance.js → finance.cjs} +39 -22
- package/dist/{fiscal.js → fiscal.cjs} +36 -17
- package/dist/{format.js → format.cjs} +83 -70
- package/dist/{healthcare.js → healthcare.cjs} +37 -22
- package/dist/{holidays.js → holidays.cjs} +52 -25
- package/dist/index.cjs +595 -0
- package/dist/{interval.js → interval.cjs} +24 -11
- package/dist/{iterate.js → iterate.cjs} +84 -41
- package/dist/{locale.js → locale.cjs} +54 -26
- package/dist/{naturalLanguage.js → naturalLanguage.cjs} +36 -23
- package/dist/naturalLanguage.d.ts +1 -3
- package/dist/naturalLanguage.d.ts.map +1 -1
- package/dist/{parse.js → parse.cjs} +24 -11
- package/dist/{performance.js → performance.cjs} +23 -10
- package/dist/{plugins.js → plugins.cjs} +48 -47
- package/dist/plugins.d.ts +0 -6
- package/dist/plugins.d.ts.map +1 -1
- package/dist/{precision.js → precision.cjs} +74 -37
- package/dist/{rangePresets.js → rangePresets.cjs} +40 -19
- package/dist/{recurrence.js → recurrence.cjs} +27 -21
- package/dist/recurrence.d.ts.map +1 -1
- package/dist/{scheduling.js → scheduling.cjs} +46 -31
- package/dist/{serialize.js → serialize.cjs} +36 -17
- package/dist/{temporal.js → temporal.cjs} +28 -13
- package/dist/{timezone.js → timezone.cjs} +140 -82
- package/dist/timezone.d.ts +6 -1
- package/dist/timezone.d.ts.map +1 -1
- package/dist/{types.js → types.cjs} +9 -3
- package/dist/types.d.ts +0 -4
- package/dist/types.d.ts.map +1 -1
- package/dist/{validate.js → validate.cjs} +54 -26
- package/dist/{workingHours.js → workingHours.cjs} +36 -17
- package/package.json +40 -37
- package/dist/constants.js +0 -16
- package/dist/index.js +0 -72
|
@@ -1,11 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
/**
|
|
2
3
|
* Calendar and holiday utilities
|
|
3
4
|
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.getWeekNumber = getWeekNumber;
|
|
7
|
+
exports.getWeekOfMonth = getWeekOfMonth;
|
|
8
|
+
exports.getQuarter = getQuarter;
|
|
9
|
+
exports.getDayOfYear = getDayOfYear;
|
|
10
|
+
exports.getWeeksInYear = getWeeksInYear;
|
|
11
|
+
exports.getDaysInMonth = getDaysInMonth;
|
|
12
|
+
exports.getDaysInYear = getDaysInYear;
|
|
13
|
+
exports.getEaster = getEaster;
|
|
14
|
+
exports.getMonthsInYear = getMonthsInYear;
|
|
15
|
+
exports.getDaysInMonthArray = getDaysInMonthArray;
|
|
16
|
+
exports.getWeekdaysInMonth = getWeekdaysInMonth;
|
|
17
|
+
exports.getFirstDayOfMonth = getFirstDayOfMonth;
|
|
18
|
+
exports.getLastDayOfMonth = getLastDayOfMonth;
|
|
19
|
+
exports.getFirstDayOfYear = getFirstDayOfYear;
|
|
20
|
+
exports.getLastDayOfYear = getLastDayOfYear;
|
|
21
|
+
exports.getNthDayOfMonth = getNthDayOfMonth;
|
|
22
|
+
exports.getNewYearsDay = getNewYearsDay;
|
|
23
|
+
exports.getMLKDay = getMLKDay;
|
|
24
|
+
exports.getPresidentsDay = getPresidentsDay;
|
|
25
|
+
exports.getMemorialDay = getMemorialDay;
|
|
26
|
+
exports.getIndependenceDay = getIndependenceDay;
|
|
27
|
+
exports.getLaborDay = getLaborDay;
|
|
28
|
+
exports.getColumbusDay = getColumbusDay;
|
|
29
|
+
exports.getVeteransDay = getVeteransDay;
|
|
30
|
+
exports.getThanksgivingDay = getThanksgivingDay;
|
|
31
|
+
exports.getChristmasDay = getChristmasDay;
|
|
32
|
+
exports.getGoodFriday = getGoodFriday;
|
|
33
|
+
exports.getUSHolidays = getUSHolidays;
|
|
34
|
+
exports.isUSHoliday = isUSHoliday;
|
|
35
|
+
exports.getUSHolidayName = getUSHolidayName;
|
|
36
|
+
exports.getStartOfWeek = getStartOfWeek;
|
|
37
|
+
exports.getEndOfWeek = getEndOfWeek;
|
|
38
|
+
exports.getWeeksInMonth = getWeeksInMonth;
|
|
39
|
+
exports.getWeekYear = getWeekYear;
|
|
40
|
+
exports.lastDayOfDecade = lastDayOfDecade;
|
|
41
|
+
exports.firstDayOfDecade = firstDayOfDecade;
|
|
42
|
+
exports.lastDayOfCentury = lastDayOfCentury;
|
|
43
|
+
exports.getStartOfQuarter = getStartOfQuarter;
|
|
44
|
+
exports.getEndOfQuarter = getEndOfQuarter;
|
|
4
45
|
/**
|
|
5
46
|
* Get the week number of the year (ISO 8601)
|
|
6
47
|
* @param date - date to get week number for
|
|
7
48
|
*/
|
|
8
|
-
|
|
49
|
+
function getWeekNumber(date) {
|
|
9
50
|
const d = new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()));
|
|
10
51
|
const dayNum = d.getUTCDay() || 7;
|
|
11
52
|
d.setUTCDate(d.getUTCDate() + 4 - dayNum);
|
|
@@ -16,7 +57,7 @@ export function getWeekNumber(date) {
|
|
|
16
57
|
* Get the week of the month (1-6)
|
|
17
58
|
* @param date - date to get week of month for
|
|
18
59
|
*/
|
|
19
|
-
|
|
60
|
+
function getWeekOfMonth(date) {
|
|
20
61
|
const firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
|
|
21
62
|
const firstWeekday = firstDay.getDay();
|
|
22
63
|
const offsetDate = date.getDate() + firstWeekday - 1;
|
|
@@ -26,14 +67,14 @@ export function getWeekOfMonth(date) {
|
|
|
26
67
|
* Get the quarter of the year (1-4)
|
|
27
68
|
* @param date - date to get quarter for
|
|
28
69
|
*/
|
|
29
|
-
|
|
70
|
+
function getQuarter(date) {
|
|
30
71
|
return Math.floor(date.getMonth() / 3) + 1;
|
|
31
72
|
}
|
|
32
73
|
/**
|
|
33
74
|
* Get the day of the year (1-366)
|
|
34
75
|
* @param date - date to get day of year for
|
|
35
76
|
*/
|
|
36
|
-
|
|
77
|
+
function getDayOfYear(date) {
|
|
37
78
|
const start = new Date(date.getFullYear(), 0, 0);
|
|
38
79
|
const diff = date.getTime() - start.getTime();
|
|
39
80
|
return Math.floor(diff / (1000 * 60 * 60 * 24));
|
|
@@ -42,7 +83,7 @@ export function getDayOfYear(date) {
|
|
|
42
83
|
* Get the number of weeks in a year
|
|
43
84
|
* @param year - year to check
|
|
44
85
|
*/
|
|
45
|
-
|
|
86
|
+
function getWeeksInYear(year) {
|
|
46
87
|
const jan1 = new Date(year, 0, 1);
|
|
47
88
|
const dec31 = new Date(year, 11, 31);
|
|
48
89
|
// If Jan 1 is Thu-Sun or Dec 31 is Mon-Wed, there are 53 weeks
|
|
@@ -58,14 +99,14 @@ export function getWeeksInYear(year) {
|
|
|
58
99
|
* @param year - year
|
|
59
100
|
* @param month - month (0-11)
|
|
60
101
|
*/
|
|
61
|
-
|
|
102
|
+
function getDaysInMonth(year, month) {
|
|
62
103
|
return new Date(year, month + 1, 0).getDate();
|
|
63
104
|
}
|
|
64
105
|
/**
|
|
65
106
|
* Get the number of days in a year
|
|
66
107
|
* @param year - year to check
|
|
67
108
|
*/
|
|
68
|
-
|
|
109
|
+
function getDaysInYear(year) {
|
|
69
110
|
return isLeapYear(year) ? 366 : 365;
|
|
70
111
|
}
|
|
71
112
|
/**
|
|
@@ -79,7 +120,7 @@ function isLeapYear(year) {
|
|
|
79
120
|
* Calculate Easter Sunday for a given year (Western/Gregorian calendar)
|
|
80
121
|
* @param year - year to calculate Easter for
|
|
81
122
|
*/
|
|
82
|
-
|
|
123
|
+
function getEaster(year) {
|
|
83
124
|
// Anonymous Gregorian algorithm
|
|
84
125
|
const a = year % 19;
|
|
85
126
|
const b = Math.floor(year / 100);
|
|
@@ -101,7 +142,7 @@ export function getEaster(year) {
|
|
|
101
142
|
* Get all months in a year
|
|
102
143
|
* @param year - year to get months for
|
|
103
144
|
*/
|
|
104
|
-
|
|
145
|
+
function getMonthsInYear(year) {
|
|
105
146
|
return Array.from({ length: 12 }, (_, i) => new Date(year, i, 1));
|
|
106
147
|
}
|
|
107
148
|
/**
|
|
@@ -109,7 +150,7 @@ export function getMonthsInYear(year) {
|
|
|
109
150
|
* @param year - year
|
|
110
151
|
* @param month - month (0-11)
|
|
111
152
|
*/
|
|
112
|
-
|
|
153
|
+
function getDaysInMonthArray(year, month) {
|
|
113
154
|
const daysCount = getDaysInMonth(year, month);
|
|
114
155
|
return Array.from({ length: daysCount }, (_, i) => new Date(year, month, i + 1));
|
|
115
156
|
}
|
|
@@ -118,7 +159,7 @@ export function getDaysInMonthArray(year, month) {
|
|
|
118
159
|
* @param year - year
|
|
119
160
|
* @param month - month (0-11)
|
|
120
161
|
*/
|
|
121
|
-
|
|
162
|
+
function getWeekdaysInMonth(year, month) {
|
|
122
163
|
return getDaysInMonthArray(year, month).filter(date => {
|
|
123
164
|
const day = date.getDay();
|
|
124
165
|
return day !== 0 && day !== 6; // Not Sunday or Saturday
|
|
@@ -128,28 +169,28 @@ export function getWeekdaysInMonth(year, month) {
|
|
|
128
169
|
* Get the first day of the month
|
|
129
170
|
* @param date - any date in the month
|
|
130
171
|
*/
|
|
131
|
-
|
|
172
|
+
function getFirstDayOfMonth(date) {
|
|
132
173
|
return new Date(date.getFullYear(), date.getMonth(), 1);
|
|
133
174
|
}
|
|
134
175
|
/**
|
|
135
176
|
* Get the last day of the month
|
|
136
177
|
* @param date - any date in the month
|
|
137
178
|
*/
|
|
138
|
-
|
|
179
|
+
function getLastDayOfMonth(date) {
|
|
139
180
|
return new Date(date.getFullYear(), date.getMonth() + 1, 0);
|
|
140
181
|
}
|
|
141
182
|
/**
|
|
142
183
|
* Get the first day of the year
|
|
143
184
|
* @param year - year
|
|
144
185
|
*/
|
|
145
|
-
|
|
186
|
+
function getFirstDayOfYear(year) {
|
|
146
187
|
return new Date(year, 0, 1);
|
|
147
188
|
}
|
|
148
189
|
/**
|
|
149
190
|
* Get the last day of the year
|
|
150
191
|
* @param year - year
|
|
151
192
|
*/
|
|
152
|
-
|
|
193
|
+
function getLastDayOfYear(year) {
|
|
153
194
|
return new Date(year, 11, 31);
|
|
154
195
|
}
|
|
155
196
|
/**
|
|
@@ -159,7 +200,7 @@ export function getLastDayOfYear(year) {
|
|
|
159
200
|
* @param dayOfWeek - day of week (0=Sunday, 6=Saturday)
|
|
160
201
|
* @param n - occurrence (1-5, or -1 for last)
|
|
161
202
|
*/
|
|
162
|
-
|
|
203
|
+
function getNthDayOfMonth(year, month, dayOfWeek, n) {
|
|
163
204
|
if (n === 0 || n < -1 || n > 5)
|
|
164
205
|
return null;
|
|
165
206
|
const firstDay = new Date(year, month, 1);
|
|
@@ -191,77 +232,77 @@ export function getNthDayOfMonth(year, month, dayOfWeek, n) {
|
|
|
191
232
|
* Get New Year's Day
|
|
192
233
|
* @param year - year
|
|
193
234
|
*/
|
|
194
|
-
|
|
235
|
+
function getNewYearsDay(year) {
|
|
195
236
|
return new Date(year, 0, 1);
|
|
196
237
|
}
|
|
197
238
|
/**
|
|
198
239
|
* Get Martin Luther King Jr. Day (3rd Monday of January)
|
|
199
240
|
* @param year - year
|
|
200
241
|
*/
|
|
201
|
-
|
|
242
|
+
function getMLKDay(year) {
|
|
202
243
|
return getNthDayOfMonth(year, 0, 1, 3);
|
|
203
244
|
}
|
|
204
245
|
/**
|
|
205
246
|
* Get Presidents' Day (3rd Monday of February)
|
|
206
247
|
* @param year - year
|
|
207
248
|
*/
|
|
208
|
-
|
|
249
|
+
function getPresidentsDay(year) {
|
|
209
250
|
return getNthDayOfMonth(year, 1, 1, 3);
|
|
210
251
|
}
|
|
211
252
|
/**
|
|
212
253
|
* Get Memorial Day (last Monday of May)
|
|
213
254
|
* @param year - year
|
|
214
255
|
*/
|
|
215
|
-
|
|
256
|
+
function getMemorialDay(year) {
|
|
216
257
|
return getNthDayOfMonth(year, 4, 1, -1);
|
|
217
258
|
}
|
|
218
259
|
/**
|
|
219
260
|
* Get Independence Day (July 4th)
|
|
220
261
|
* @param year - year
|
|
221
262
|
*/
|
|
222
|
-
|
|
263
|
+
function getIndependenceDay(year) {
|
|
223
264
|
return new Date(year, 6, 4);
|
|
224
265
|
}
|
|
225
266
|
/**
|
|
226
267
|
* Get Labor Day (1st Monday of September)
|
|
227
268
|
* @param year - year
|
|
228
269
|
*/
|
|
229
|
-
|
|
270
|
+
function getLaborDay(year) {
|
|
230
271
|
return getNthDayOfMonth(year, 8, 1, 1);
|
|
231
272
|
}
|
|
232
273
|
/**
|
|
233
274
|
* Get Columbus Day (2nd Monday of October)
|
|
234
275
|
* @param year - year
|
|
235
276
|
*/
|
|
236
|
-
|
|
277
|
+
function getColumbusDay(year) {
|
|
237
278
|
return getNthDayOfMonth(year, 9, 1, 2);
|
|
238
279
|
}
|
|
239
280
|
/**
|
|
240
281
|
* Get Veterans Day (November 11th)
|
|
241
282
|
* @param year - year
|
|
242
283
|
*/
|
|
243
|
-
|
|
284
|
+
function getVeteransDay(year) {
|
|
244
285
|
return new Date(year, 10, 11);
|
|
245
286
|
}
|
|
246
287
|
/**
|
|
247
288
|
* Get Thanksgiving Day (4th Thursday of November)
|
|
248
289
|
* @param year - year
|
|
249
290
|
*/
|
|
250
|
-
|
|
291
|
+
function getThanksgivingDay(year) {
|
|
251
292
|
return getNthDayOfMonth(year, 10, 4, 4);
|
|
252
293
|
}
|
|
253
294
|
/**
|
|
254
295
|
* Get Christmas Day (December 25th)
|
|
255
296
|
* @param year - year
|
|
256
297
|
*/
|
|
257
|
-
|
|
298
|
+
function getChristmasDay(year) {
|
|
258
299
|
return new Date(year, 11, 25);
|
|
259
300
|
}
|
|
260
301
|
/**
|
|
261
302
|
* Get Good Friday (Friday before Easter)
|
|
262
303
|
* @param year - year
|
|
263
304
|
*/
|
|
264
|
-
|
|
305
|
+
function getGoodFriday(year) {
|
|
265
306
|
const easter = getEaster(year);
|
|
266
307
|
const goodFriday = new Date(easter);
|
|
267
308
|
goodFriday.setDate(easter.getDate() - 2);
|
|
@@ -271,7 +312,7 @@ export function getGoodFriday(year) {
|
|
|
271
312
|
* Get all US federal holidays for a year
|
|
272
313
|
* @param year - year
|
|
273
314
|
*/
|
|
274
|
-
|
|
315
|
+
function getUSHolidays(year) {
|
|
275
316
|
const holidays = [];
|
|
276
317
|
holidays.push({ name: "New Year's Day", date: getNewYearsDay(year), type: 'federal' });
|
|
277
318
|
const mlk = getMLKDay(year);
|
|
@@ -301,7 +342,7 @@ export function getUSHolidays(year) {
|
|
|
301
342
|
* Check if a date is a US federal holiday
|
|
302
343
|
* @param date - date to check
|
|
303
344
|
*/
|
|
304
|
-
|
|
345
|
+
function isUSHoliday(date) {
|
|
305
346
|
const holidays = getUSHolidays(date.getFullYear());
|
|
306
347
|
return holidays.some(h => h.date.getFullYear() === date.getFullYear() &&
|
|
307
348
|
h.date.getMonth() === date.getMonth() &&
|
|
@@ -312,7 +353,7 @@ export function isUSHoliday(date) {
|
|
|
312
353
|
* @param date - date to check
|
|
313
354
|
* @returns holiday name or null if not a holiday
|
|
314
355
|
*/
|
|
315
|
-
|
|
356
|
+
function getUSHolidayName(date) {
|
|
316
357
|
const holidays = getUSHolidays(date.getFullYear());
|
|
317
358
|
const holiday = holidays.find(h => h.date.getFullYear() === date.getFullYear() &&
|
|
318
359
|
h.date.getMonth() === date.getMonth() &&
|
|
@@ -323,7 +364,7 @@ export function getUSHolidayName(date) {
|
|
|
323
364
|
* Get the start of the week for a date (Monday)
|
|
324
365
|
* @param date - any date
|
|
325
366
|
*/
|
|
326
|
-
|
|
367
|
+
function getStartOfWeek(date) {
|
|
327
368
|
const d = new Date(date);
|
|
328
369
|
const day = d.getDay();
|
|
329
370
|
const diff = d.getDate() - day + (day === 0 ? -6 : 1); // Adjust for Monday start
|
|
@@ -335,7 +376,7 @@ export function getStartOfWeek(date) {
|
|
|
335
376
|
* Get the end of the week for a date (Sunday)
|
|
336
377
|
* @param date - any date
|
|
337
378
|
*/
|
|
338
|
-
|
|
379
|
+
function getEndOfWeek(date) {
|
|
339
380
|
const start = getStartOfWeek(date);
|
|
340
381
|
const end = new Date(start);
|
|
341
382
|
end.setDate(start.getDate() + 6);
|
|
@@ -347,7 +388,7 @@ export function getEndOfWeek(date) {
|
|
|
347
388
|
* @param year - year
|
|
348
389
|
* @param month - month (0-11)
|
|
349
390
|
*/
|
|
350
|
-
|
|
391
|
+
function getWeeksInMonth(year, month) {
|
|
351
392
|
const weeks = [];
|
|
352
393
|
const firstDay = new Date(year, month, 1);
|
|
353
394
|
const lastDay = new Date(year, month + 1, 0);
|
|
@@ -384,7 +425,7 @@ export function getWeeksInMonth(year, month) {
|
|
|
384
425
|
* getWeekYear(new Date('2024-01-01')) // 2024
|
|
385
426
|
* getWeekYear(new Date('2020-12-31')) // 2020 (belongs to week 53 of 2020)
|
|
386
427
|
*/
|
|
387
|
-
|
|
428
|
+
function getWeekYear(date) {
|
|
388
429
|
const d = new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()));
|
|
389
430
|
const dayNum = d.getUTCDay() || 7;
|
|
390
431
|
d.setUTCDate(d.getUTCDate() + 4 - dayNum);
|
|
@@ -397,7 +438,7 @@ export function getWeekYear(date) {
|
|
|
397
438
|
* @example
|
|
398
439
|
* lastDayOfDecade(new Date('2024-06-15')) // 2029-12-31
|
|
399
440
|
*/
|
|
400
|
-
|
|
441
|
+
function lastDayOfDecade(date) {
|
|
401
442
|
const year = date.getFullYear();
|
|
402
443
|
const decadeEnd = Math.floor(year / 10) * 10 + 9;
|
|
403
444
|
return new Date(decadeEnd, 11, 31);
|
|
@@ -409,7 +450,7 @@ export function lastDayOfDecade(date) {
|
|
|
409
450
|
* @example
|
|
410
451
|
* firstDayOfDecade(new Date('2024-06-15')) // 2020-01-01
|
|
411
452
|
*/
|
|
412
|
-
|
|
453
|
+
function firstDayOfDecade(date) {
|
|
413
454
|
const year = date.getFullYear();
|
|
414
455
|
const decadeStart = Math.floor(year / 10) * 10;
|
|
415
456
|
return new Date(decadeStart, 0, 1);
|
|
@@ -421,7 +462,7 @@ export function firstDayOfDecade(date) {
|
|
|
421
462
|
* @example
|
|
422
463
|
* lastDayOfCentury(new Date('2024-06-15')) // 2099-12-31
|
|
423
464
|
*/
|
|
424
|
-
|
|
465
|
+
function lastDayOfCentury(date) {
|
|
425
466
|
const year = date.getFullYear();
|
|
426
467
|
const centuryEnd = Math.floor(year / 100) * 100 + 99;
|
|
427
468
|
return new Date(centuryEnd, 11, 31);
|
|
@@ -431,7 +472,7 @@ export function lastDayOfCentury(date) {
|
|
|
431
472
|
* @param date - any date
|
|
432
473
|
* @returns First day of the quarter
|
|
433
474
|
*/
|
|
434
|
-
|
|
475
|
+
function getStartOfQuarter(date) {
|
|
435
476
|
const month = Math.floor(date.getMonth() / 3) * 3;
|
|
436
477
|
return new Date(date.getFullYear(), month, 1);
|
|
437
478
|
}
|
|
@@ -440,7 +481,7 @@ export function getStartOfQuarter(date) {
|
|
|
440
481
|
* @param date - any date
|
|
441
482
|
* @returns Last day of the quarter
|
|
442
483
|
*/
|
|
443
|
-
|
|
484
|
+
function getEndOfQuarter(date) {
|
|
444
485
|
const month = Math.floor(date.getMonth() / 3) * 3 + 2;
|
|
445
486
|
return new Date(date.getFullYear(), month + 1, 0, 23, 59, 59, 999);
|
|
446
487
|
}
|
|
@@ -1,7 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
/**
|
|
2
3
|
* @fileoverview Non-Gregorian calendar conversions using Intl.DateTimeFormat
|
|
3
4
|
* Supports Hebrew, Islamic, Buddhist, Japanese, Persian, and Chinese calendars
|
|
4
5
|
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.toHebrewDate = toHebrewDate;
|
|
8
|
+
exports.toIslamicDate = toIslamicDate;
|
|
9
|
+
exports.toBuddhistDate = toBuddhistDate;
|
|
10
|
+
exports.toJapaneseDate = toJapaneseDate;
|
|
11
|
+
exports.toPersianDate = toPersianDate;
|
|
12
|
+
exports.toChineseDate = toChineseDate;
|
|
13
|
+
exports.formatInCalendar = formatInCalendar;
|
|
14
|
+
exports.getCalendarMonthNames = getCalendarMonthNames;
|
|
15
|
+
exports.getJapaneseEra = getJapaneseEra;
|
|
16
|
+
exports.getJapaneseEras = getJapaneseEras;
|
|
17
|
+
exports.isHebrewLeapYear = isHebrewLeapYear;
|
|
18
|
+
exports.getHebrewMonthName = getHebrewMonthName;
|
|
19
|
+
exports.getIslamicMonthName = getIslamicMonthName;
|
|
20
|
+
exports.getPersianMonthName = getPersianMonthName;
|
|
21
|
+
exports.isPersianLeapYear = isPersianLeapYear;
|
|
22
|
+
exports.getChineseZodiac = getChineseZodiac;
|
|
23
|
+
exports.getChineseElement = getChineseElement;
|
|
24
|
+
exports.getChineseZodiacFull = getChineseZodiacFull;
|
|
25
|
+
exports.calendarDateToString = calendarDateToString;
|
|
26
|
+
exports.compareCalendarDates = compareCalendarDates;
|
|
27
|
+
exports.today = today;
|
|
28
|
+
exports.isSameCalendarDay = isSameCalendarDay;
|
|
29
|
+
exports.getSupportedCalendars = getSupportedCalendars;
|
|
5
30
|
/**
|
|
6
31
|
* Extracts calendar date parts using Intl.DateTimeFormat
|
|
7
32
|
*/
|
|
@@ -96,7 +121,7 @@ function getMonthIndexFromName(monthName, calendar) {
|
|
|
96
121
|
* Convert Gregorian date to Hebrew calendar
|
|
97
122
|
* @example toHebrewDate(new Date('2024-03-25')) // { year: 5784, month: 6, day: 15, calendar: 'hebrew' }
|
|
98
123
|
*/
|
|
99
|
-
|
|
124
|
+
function toHebrewDate(date) {
|
|
100
125
|
return extractCalendarParts(date, 'hebrew');
|
|
101
126
|
}
|
|
102
127
|
/**
|
|
@@ -105,7 +130,7 @@ export function toHebrewDate(date) {
|
|
|
105
130
|
* @param variant - Islamic calendar variant: 'islamic', 'islamic-umalqura', 'islamic-civil'
|
|
106
131
|
* @example toIslamicDate(new Date('2024-03-25')) // { year: 1445, month: 9, day: 15, calendar: 'islamic-umalqura' }
|
|
107
132
|
*/
|
|
108
|
-
|
|
133
|
+
function toIslamicDate(date, variant = 'islamic-umalqura') {
|
|
109
134
|
return extractCalendarParts(date, variant);
|
|
110
135
|
}
|
|
111
136
|
/**
|
|
@@ -113,14 +138,14 @@ export function toIslamicDate(date, variant = 'islamic-umalqura') {
|
|
|
113
138
|
* Buddhist Era = Gregorian Year + 543
|
|
114
139
|
* @example toBuddhistDate(new Date('2024-03-25')) // { year: 2567, month: 3, day: 25, calendar: 'buddhist' }
|
|
115
140
|
*/
|
|
116
|
-
|
|
141
|
+
function toBuddhistDate(date) {
|
|
117
142
|
return extractCalendarParts(date, 'buddhist');
|
|
118
143
|
}
|
|
119
144
|
/**
|
|
120
145
|
* Convert Gregorian date to Japanese calendar with era
|
|
121
146
|
* @example toJapaneseDate(new Date('2024-03-25')) // { year: 6, month: 3, day: 25, era: 'Reiwa', calendar: 'japanese' }
|
|
122
147
|
*/
|
|
123
|
-
|
|
148
|
+
function toJapaneseDate(date) {
|
|
124
149
|
const result = extractCalendarParts(date, 'japanese');
|
|
125
150
|
// Get full era name
|
|
126
151
|
const eraFormatter = new Intl.DateTimeFormat('en-u-ca-japanese', { era: 'long' });
|
|
@@ -135,14 +160,14 @@ export function toJapaneseDate(date) {
|
|
|
135
160
|
* Convert Gregorian date to Persian (Jalali/Solar Hijri) calendar
|
|
136
161
|
* @example toPersianDate(new Date('2024-03-20')) // { year: 1403, month: 1, day: 1, calendar: 'persian' }
|
|
137
162
|
*/
|
|
138
|
-
|
|
163
|
+
function toPersianDate(date) {
|
|
139
164
|
return extractCalendarParts(date, 'persian');
|
|
140
165
|
}
|
|
141
166
|
/**
|
|
142
167
|
* Convert Gregorian date to Chinese lunar calendar
|
|
143
168
|
* @example toChineseDate(new Date('2024-02-10')) // { year: 4721, month: 1, day: 1, calendar: 'chinese' }
|
|
144
169
|
*/
|
|
145
|
-
|
|
170
|
+
function toChineseDate(date) {
|
|
146
171
|
return extractCalendarParts(date, 'chinese');
|
|
147
172
|
}
|
|
148
173
|
/**
|
|
@@ -152,7 +177,7 @@ export function toChineseDate(date) {
|
|
|
152
177
|
* @param locale - Locale for formatting (default: 'en')
|
|
153
178
|
* @param options - Additional Intl.DateTimeFormat options
|
|
154
179
|
*/
|
|
155
|
-
|
|
180
|
+
function formatInCalendar(date, calendar, locale = 'en', options = {}) {
|
|
156
181
|
const localeWithCalendar = `${locale}-u-ca-${calendar}`;
|
|
157
182
|
const formatter = new Intl.DateTimeFormat(localeWithCalendar, {
|
|
158
183
|
dateStyle: 'long',
|
|
@@ -166,7 +191,7 @@ export function formatInCalendar(date, calendar, locale = 'en', options = {}) {
|
|
|
166
191
|
* @param locale - Locale for month names (default: 'en')
|
|
167
192
|
* @param format - Month name format: 'long', 'short', 'narrow'
|
|
168
193
|
*/
|
|
169
|
-
|
|
194
|
+
function getCalendarMonthNames(calendar, locale = 'en', format = 'long') {
|
|
170
195
|
const localeWithCalendar = `${locale}-u-ca-${calendar}`;
|
|
171
196
|
const formatter = new Intl.DateTimeFormat(localeWithCalendar, { month: format });
|
|
172
197
|
// Generate month names by iterating through a reference year
|
|
@@ -190,7 +215,7 @@ export function getCalendarMonthNames(calendar, locale = 'en', format = 'long')
|
|
|
190
215
|
* @param date - Date to get era for
|
|
191
216
|
* @param format - Era format: 'long', 'short', 'narrow'
|
|
192
217
|
*/
|
|
193
|
-
|
|
218
|
+
function getJapaneseEra(date, format = 'long') {
|
|
194
219
|
const formatter = new Intl.DateTimeFormat('en-u-ca-japanese', { era: format });
|
|
195
220
|
const parts = formatter.formatToParts(date);
|
|
196
221
|
const eraPart = parts.find(p => p.type === 'era');
|
|
@@ -199,7 +224,7 @@ export function getJapaneseEra(date, format = 'long') {
|
|
|
199
224
|
/**
|
|
200
225
|
* Get all Japanese era names with their start dates
|
|
201
226
|
*/
|
|
202
|
-
|
|
227
|
+
function getJapaneseEras() {
|
|
203
228
|
return [
|
|
204
229
|
{ name: 'Meiji', start: new Date(1868, 9, 23) },
|
|
205
230
|
{ name: 'Taisho', start: new Date(1912, 6, 30) },
|
|
@@ -212,7 +237,7 @@ export function getJapaneseEras() {
|
|
|
212
237
|
* Check if a Hebrew year is a leap year (has 13 months)
|
|
213
238
|
* @param hebrewYear - Year in Hebrew calendar
|
|
214
239
|
*/
|
|
215
|
-
|
|
240
|
+
function isHebrewLeapYear(hebrewYear) {
|
|
216
241
|
// Hebrew leap years follow a 19-year cycle
|
|
217
242
|
// Years 3, 6, 8, 11, 14, 17, 19 are leap years
|
|
218
243
|
const position = hebrewYear % 19;
|
|
@@ -223,7 +248,7 @@ export function isHebrewLeapYear(hebrewYear) {
|
|
|
223
248
|
* @param month - Month number (1-13)
|
|
224
249
|
* @param isLeapYear - Whether the year is a leap year
|
|
225
250
|
*/
|
|
226
|
-
|
|
251
|
+
function getHebrewMonthName(month, isLeapYear = false) {
|
|
227
252
|
const months = [
|
|
228
253
|
'Nisan', 'Iyar', 'Sivan', 'Tammuz', 'Av', 'Elul',
|
|
229
254
|
'Tishrei', 'Cheshvan', 'Kislev', 'Tevet', 'Shevat',
|
|
@@ -236,7 +261,7 @@ export function getHebrewMonthName(month, isLeapYear = false) {
|
|
|
236
261
|
* Get Islamic month name
|
|
237
262
|
* @param month - Month number (1-12)
|
|
238
263
|
*/
|
|
239
|
-
|
|
264
|
+
function getIslamicMonthName(month) {
|
|
240
265
|
const months = [
|
|
241
266
|
'Muharram', 'Safar', 'Rabi\' al-Awwal', 'Rabi\' al-Thani',
|
|
242
267
|
'Jumada al-Awwal', 'Jumada al-Thani', 'Rajab', 'Sha\'ban',
|
|
@@ -248,7 +273,7 @@ export function getIslamicMonthName(month) {
|
|
|
248
273
|
* Get Persian month name
|
|
249
274
|
* @param month - Month number (1-12)
|
|
250
275
|
*/
|
|
251
|
-
|
|
276
|
+
function getPersianMonthName(month) {
|
|
252
277
|
const months = [
|
|
253
278
|
'Farvardin', 'Ordibehesht', 'Khordad', 'Tir', 'Mordad', 'Shahrivar',
|
|
254
279
|
'Mehr', 'Aban', 'Azar', 'Dey', 'Bahman', 'Esfand'
|
|
@@ -259,7 +284,7 @@ export function getPersianMonthName(month) {
|
|
|
259
284
|
* Check if a Persian year is a leap year
|
|
260
285
|
* Uses the 2820-year cycle algorithm
|
|
261
286
|
*/
|
|
262
|
-
|
|
287
|
+
function isPersianLeapYear(persianYear) {
|
|
263
288
|
// Simplified 33-year cycle approximation
|
|
264
289
|
const remainder = persianYear % 33;
|
|
265
290
|
return [1, 5, 9, 13, 17, 22, 26, 30].includes(remainder);
|
|
@@ -268,7 +293,7 @@ export function isPersianLeapYear(persianYear) {
|
|
|
268
293
|
* Get Chinese zodiac animal for a year
|
|
269
294
|
* @param gregorianYear - Gregorian year
|
|
270
295
|
*/
|
|
271
|
-
|
|
296
|
+
function getChineseZodiac(gregorianYear) {
|
|
272
297
|
const animals = [
|
|
273
298
|
'Rat', 'Ox', 'Tiger', 'Rabbit', 'Dragon', 'Snake',
|
|
274
299
|
'Horse', 'Goat', 'Monkey', 'Rooster', 'Dog', 'Pig'
|
|
@@ -281,7 +306,7 @@ export function getChineseZodiac(gregorianYear) {
|
|
|
281
306
|
* Get Chinese element for a year
|
|
282
307
|
* @param gregorianYear - Gregorian year
|
|
283
308
|
*/
|
|
284
|
-
|
|
309
|
+
function getChineseElement(gregorianYear) {
|
|
285
310
|
const elements = ['Metal', 'Water', 'Wood', 'Fire', 'Earth'];
|
|
286
311
|
// Each element covers 2 years
|
|
287
312
|
const index = Math.floor(((gregorianYear - 1900) % 10) / 2);
|
|
@@ -291,13 +316,13 @@ export function getChineseElement(gregorianYear) {
|
|
|
291
316
|
* Get full Chinese zodiac description (element + animal)
|
|
292
317
|
* @param gregorianYear - Gregorian year
|
|
293
318
|
*/
|
|
294
|
-
|
|
319
|
+
function getChineseZodiacFull(gregorianYear) {
|
|
295
320
|
return `${getChineseElement(gregorianYear)} ${getChineseZodiac(gregorianYear)}`;
|
|
296
321
|
}
|
|
297
322
|
/**
|
|
298
323
|
* Convert calendar date to string representation
|
|
299
324
|
*/
|
|
300
|
-
|
|
325
|
+
function calendarDateToString(calendarDate) {
|
|
301
326
|
const { year, month, day, era, calendar } = calendarDate;
|
|
302
327
|
const eraStr = era ? ` ${era}` : '';
|
|
303
328
|
return `${year}/${month}/${day}${eraStr} (${calendar})`;
|
|
@@ -306,7 +331,7 @@ export function calendarDateToString(calendarDate) {
|
|
|
306
331
|
* Compare two calendar dates
|
|
307
332
|
* @returns negative if a < b, 0 if equal, positive if a > b
|
|
308
333
|
*/
|
|
309
|
-
|
|
334
|
+
function compareCalendarDates(a, b) {
|
|
310
335
|
if (a.calendar !== b.calendar) {
|
|
311
336
|
throw new Error('Cannot compare dates from different calendar systems');
|
|
312
337
|
}
|
|
@@ -319,13 +344,13 @@ export function compareCalendarDates(a, b) {
|
|
|
319
344
|
/**
|
|
320
345
|
* Get current date in specified calendar
|
|
321
346
|
*/
|
|
322
|
-
|
|
347
|
+
function today(calendar) {
|
|
323
348
|
return extractCalendarParts(new Date(), calendar);
|
|
324
349
|
}
|
|
325
350
|
/**
|
|
326
351
|
* Check if two calendar dates are the same day
|
|
327
352
|
*/
|
|
328
|
-
|
|
353
|
+
function isSameCalendarDay(a, b) {
|
|
329
354
|
return a.calendar === b.calendar &&
|
|
330
355
|
a.year === b.year &&
|
|
331
356
|
a.month === b.month &&
|
|
@@ -334,7 +359,7 @@ export function isSameCalendarDay(a, b) {
|
|
|
334
359
|
/**
|
|
335
360
|
* Get supported calendar systems
|
|
336
361
|
*/
|
|
337
|
-
|
|
362
|
+
function getSupportedCalendars() {
|
|
338
363
|
return [
|
|
339
364
|
'hebrew',
|
|
340
365
|
'islamic',
|