ts-time-utils 3.0.0 → 4.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (68) hide show
  1. package/README.md +401 -1329
  2. package/dist/calculate.d.ts +25 -0
  3. package/dist/calculate.d.ts.map +1 -1
  4. package/dist/calculate.js +125 -0
  5. package/dist/calendar.d.ts +45 -0
  6. package/dist/calendar.d.ts.map +1 -1
  7. package/dist/calendar.js +68 -0
  8. package/dist/calendars.d.ts +156 -0
  9. package/dist/calendars.d.ts.map +1 -0
  10. package/dist/calendars.js +348 -0
  11. package/dist/compare.d.ts +27 -0
  12. package/dist/compare.d.ts.map +1 -1
  13. package/dist/compare.js +46 -0
  14. package/dist/esm/calculate.d.ts +25 -0
  15. package/dist/esm/calculate.d.ts.map +1 -1
  16. package/dist/esm/calculate.js +125 -0
  17. package/dist/esm/calendar.d.ts +45 -0
  18. package/dist/esm/calendar.d.ts.map +1 -1
  19. package/dist/esm/calendar.js +68 -0
  20. package/dist/esm/calendars.d.ts +156 -0
  21. package/dist/esm/calendars.d.ts.map +1 -0
  22. package/dist/esm/calendars.js +348 -0
  23. package/dist/esm/compare.d.ts +27 -0
  24. package/dist/esm/compare.d.ts.map +1 -1
  25. package/dist/esm/compare.js +46 -0
  26. package/dist/esm/holidays.d.ts +11 -1
  27. package/dist/esm/holidays.d.ts.map +1 -1
  28. package/dist/esm/holidays.js +220 -1
  29. package/dist/esm/index.d.ts +13 -7
  30. package/dist/esm/index.d.ts.map +1 -1
  31. package/dist/esm/index.js +17 -9
  32. package/dist/esm/iterate.d.ts +55 -0
  33. package/dist/esm/iterate.d.ts.map +1 -1
  34. package/dist/esm/iterate.js +86 -0
  35. package/dist/esm/locale.d.ts +53 -0
  36. package/dist/esm/locale.d.ts.map +1 -1
  37. package/dist/esm/locale.js +141 -0
  38. package/dist/esm/precision.d.ts +225 -0
  39. package/dist/esm/precision.d.ts.map +1 -0
  40. package/dist/esm/precision.js +491 -0
  41. package/dist/esm/temporal.d.ts +237 -0
  42. package/dist/esm/temporal.d.ts.map +1 -0
  43. package/dist/esm/temporal.js +660 -0
  44. package/dist/esm/validate.d.ts +30 -0
  45. package/dist/esm/validate.d.ts.map +1 -1
  46. package/dist/esm/validate.js +48 -0
  47. package/dist/holidays.d.ts +11 -1
  48. package/dist/holidays.d.ts.map +1 -1
  49. package/dist/holidays.js +220 -1
  50. package/dist/index.d.ts +13 -7
  51. package/dist/index.d.ts.map +1 -1
  52. package/dist/index.js +17 -9
  53. package/dist/iterate.d.ts +55 -0
  54. package/dist/iterate.d.ts.map +1 -1
  55. package/dist/iterate.js +86 -0
  56. package/dist/locale.d.ts +53 -0
  57. package/dist/locale.d.ts.map +1 -1
  58. package/dist/locale.js +141 -0
  59. package/dist/precision.d.ts +225 -0
  60. package/dist/precision.d.ts.map +1 -0
  61. package/dist/precision.js +491 -0
  62. package/dist/temporal.d.ts +237 -0
  63. package/dist/temporal.d.ts.map +1 -0
  64. package/dist/temporal.js +660 -0
  65. package/dist/validate.d.ts +30 -0
  66. package/dist/validate.d.ts.map +1 -1
  67. package/dist/validate.js +48 -0
  68. package/package.json +22 -2
@@ -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,CA4BnG;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,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AO,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/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
+ }
@@ -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,IAAI,CAkB5C;AAED;;;GAGG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,EAAE,CAEpD;AAED;;;;GAIG;AACH,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,MAAM,EAAE,K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gBAAgB,CAAC,IAAI,EAAE,IAAI,GAAG,MAAM,GAAG,IAAI,CAQ1D;AAED;;;GAGG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,IAAI,GAAG,IAAI,CAO/C;AAED;;;GAGG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,IAAI,GAAG,IAAI,CAM7C;AAED;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI,EAAE,EAAE,CAkCrE;AAED;;;;;;;GAOG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,IAAI,GAAG,MAAM,CAK9C;AAED;;;;;;GAMG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,IAAI,GAAG,IAAI,CAIhD;AAED;;;;;;GAMG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,IAAI,GAAG,IAAI,CAIjD;AAED;;;;;;GAMG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,IAAI,GAAG,IAAI,CAIjD;AAED;;;;GAIG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,IAAI,GAAG,IAAI,CAGlD;AAED;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,IAAI,GAAG,IAAI,CAGhD"}
package/dist/calendar.js CHANGED
@@ -376,3 +376,71 @@ export function getWeeksInMonth(year, month) {
376
376
  }
377
377
  return weeks;
378
378
  }
379
+ /**
380
+ * Get the ISO week-numbering year (year the week belongs to)
381
+ * @param date - date to get week year for
382
+ * @returns The year the ISO week belongs to (may differ from calendar year)
383
+ * @example
384
+ * getWeekYear(new Date('2024-01-01')) // 2024
385
+ * getWeekYear(new Date('2020-12-31')) // 2020 (belongs to week 53 of 2020)
386
+ */
387
+ export function getWeekYear(date) {
388
+ const d = new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()));
389
+ const dayNum = d.getUTCDay() || 7;
390
+ d.setUTCDate(d.getUTCDate() + 4 - dayNum);
391
+ return d.getUTCFullYear();
392
+ }
393
+ /**
394
+ * Get the last day of the decade containing the date
395
+ * @param date - any date
396
+ * @returns December 31 of the last year of the decade
397
+ * @example
398
+ * lastDayOfDecade(new Date('2024-06-15')) // 2029-12-31
399
+ */
400
+ export function lastDayOfDecade(date) {
401
+ const year = date.getFullYear();
402
+ const decadeEnd = Math.floor(year / 10) * 10 + 9;
403
+ return new Date(decadeEnd, 11, 31);
404
+ }
405
+ /**
406
+ * Get the first day of the decade containing the date
407
+ * @param date - any date
408
+ * @returns January 1 of the first year of the decade
409
+ * @example
410
+ * firstDayOfDecade(new Date('2024-06-15')) // 2020-01-01
411
+ */
412
+ export function firstDayOfDecade(date) {
413
+ const year = date.getFullYear();
414
+ const decadeStart = Math.floor(year / 10) * 10;
415
+ return new Date(decadeStart, 0, 1);
416
+ }
417
+ /**
418
+ * Get the last day of the century containing the date
419
+ * @param date - any date
420
+ * @returns December 31 of the last year of the century
421
+ * @example
422
+ * lastDayOfCentury(new Date('2024-06-15')) // 2099-12-31
423
+ */
424
+ export function lastDayOfCentury(date) {
425
+ const year = date.getFullYear();
426
+ const centuryEnd = Math.floor(year / 100) * 100 + 99;
427
+ return new Date(centuryEnd, 11, 31);
428
+ }
429
+ /**
430
+ * Get the start of a quarter
431
+ * @param date - any date
432
+ * @returns First day of the quarter
433
+ */
434
+ export function getStartOfQuarter(date) {
435
+ const month = Math.floor(date.getMonth() / 3) * 3;
436
+ return new Date(date.getFullYear(), month, 1);
437
+ }
438
+ /**
439
+ * Get the end of a quarter
440
+ * @param date - any date
441
+ * @returns Last day of the quarter
442
+ */
443
+ export function getEndOfQuarter(date) {
444
+ const month = Math.floor(date.getMonth() / 3) * 3 + 2;
445
+ return new Date(date.getFullYear(), month + 1, 0, 23, 59, 59, 999);
446
+ }
@@ -0,0 +1,156 @@
1
+ /**
2
+ * @fileoverview Non-Gregorian calendar conversions using Intl.DateTimeFormat
3
+ * Supports Hebrew, Islamic, Buddhist, Japanese, Persian, and Chinese calendars
4
+ */
5
+ export interface CalendarDate {
6
+ year: number;
7
+ month: number;
8
+ day: number;
9
+ era?: string;
10
+ calendar: string;
11
+ }
12
+ export interface HebrewDate extends CalendarDate {
13
+ calendar: 'hebrew';
14
+ }
15
+ export interface IslamicDate extends CalendarDate {
16
+ calendar: 'islamic' | 'islamic-umalqura' | 'islamic-civil';
17
+ }
18
+ export interface BuddhistDate extends CalendarDate {
19
+ calendar: 'buddhist';
20
+ }
21
+ export interface JapaneseDate extends CalendarDate {
22
+ calendar: 'japanese';
23
+ era: string;
24
+ }
25
+ export interface PersianDate extends CalendarDate {
26
+ calendar: 'persian';
27
+ }
28
+ export interface ChineseDate extends CalendarDate {
29
+ calendar: 'chinese';
30
+ cycleYear?: number;
31
+ }
32
+ export type CalendarType = 'hebrew' | 'islamic' | 'islamic-umalqura' | 'islamic-civil' | 'buddhist' | 'japanese' | 'persian' | 'chinese';
33
+ /**
34
+ * Convert Gregorian date to Hebrew calendar
35
+ * @example toHebrewDate(new Date('2024-03-25')) // { year: 5784, month: 6, day: 15, calendar: 'hebrew' }
36
+ */
37
+ export declare function toHebrewDate(date: Date): HebrewDate;
38
+ /**
39
+ * Convert Gregorian date to Islamic calendar (default: islamic-umalqura)
40
+ * @param date - Date to convert
41
+ * @param variant - Islamic calendar variant: 'islamic', 'islamic-umalqura', 'islamic-civil'
42
+ * @example toIslamicDate(new Date('2024-03-25')) // { year: 1445, month: 9, day: 15, calendar: 'islamic-umalqura' }
43
+ */
44
+ export declare function toIslamicDate(date: Date, variant?: 'islamic' | 'islamic-umalqura' | 'islamic-civil'): IslamicDate;
45
+ /**
46
+ * Convert Gregorian date to Buddhist calendar (Thai Solar)
47
+ * Buddhist Era = Gregorian Year + 543
48
+ * @example toBuddhistDate(new Date('2024-03-25')) // { year: 2567, month: 3, day: 25, calendar: 'buddhist' }
49
+ */
50
+ export declare function toBuddhistDate(date: Date): BuddhistDate;
51
+ /**
52
+ * Convert Gregorian date to Japanese calendar with era
53
+ * @example toJapaneseDate(new Date('2024-03-25')) // { year: 6, month: 3, day: 25, era: 'Reiwa', calendar: 'japanese' }
54
+ */
55
+ export declare function toJapaneseDate(date: Date): JapaneseDate;
56
+ /**
57
+ * Convert Gregorian date to Persian (Jalali/Solar Hijri) calendar
58
+ * @example toPersianDate(new Date('2024-03-20')) // { year: 1403, month: 1, day: 1, calendar: 'persian' }
59
+ */
60
+ export declare function toPersianDate(date: Date): PersianDate;
61
+ /**
62
+ * Convert Gregorian date to Chinese lunar calendar
63
+ * @example toChineseDate(new Date('2024-02-10')) // { year: 4721, month: 1, day: 1, calendar: 'chinese' }
64
+ */
65
+ export declare function toChineseDate(date: Date): ChineseDate;
66
+ /**
67
+ * Format date in specified calendar system
68
+ * @param date - Date to format
69
+ * @param calendar - Calendar system to use
70
+ * @param locale - Locale for formatting (default: 'en')
71
+ * @param options - Additional Intl.DateTimeFormat options
72
+ */
73
+ export declare function formatInCalendar(date: Date, calendar: CalendarType, locale?: string, options?: Intl.DateTimeFormatOptions): string;
74
+ /**
75
+ * Get month names for a specific calendar system
76
+ * @param calendar - Calendar system
77
+ * @param locale - Locale for month names (default: 'en')
78
+ * @param format - Month name format: 'long', 'short', 'narrow'
79
+ */
80
+ export declare function getCalendarMonthNames(calendar: CalendarType, locale?: string, format?: 'long' | 'short' | 'narrow'): string[];
81
+ /**
82
+ * Get era name for Japanese calendar
83
+ * @param date - Date to get era for
84
+ * @param format - Era format: 'long', 'short', 'narrow'
85
+ */
86
+ export declare function getJapaneseEra(date: Date, format?: 'long' | 'short' | 'narrow'): string;
87
+ /**
88
+ * Get all Japanese era names with their start dates
89
+ */
90
+ export declare function getJapaneseEras(): Array<{
91
+ name: string;
92
+ start: Date;
93
+ }>;
94
+ /**
95
+ * Check if a Hebrew year is a leap year (has 13 months)
96
+ * @param hebrewYear - Year in Hebrew calendar
97
+ */
98
+ export declare function isHebrewLeapYear(hebrewYear: number): boolean;
99
+ /**
100
+ * Get Hebrew month name
101
+ * @param month - Month number (1-13)
102
+ * @param isLeapYear - Whether the year is a leap year
103
+ */
104
+ export declare function getHebrewMonthName(month: number, isLeapYear?: boolean): string;
105
+ /**
106
+ * Get Islamic month name
107
+ * @param month - Month number (1-12)
108
+ */
109
+ export declare function getIslamicMonthName(month: number): string;
110
+ /**
111
+ * Get Persian month name
112
+ * @param month - Month number (1-12)
113
+ */
114
+ export declare function getPersianMonthName(month: number): string;
115
+ /**
116
+ * Check if a Persian year is a leap year
117
+ * Uses the 2820-year cycle algorithm
118
+ */
119
+ export declare function isPersianLeapYear(persianYear: number): boolean;
120
+ /**
121
+ * Get Chinese zodiac animal for a year
122
+ * @param gregorianYear - Gregorian year
123
+ */
124
+ export declare function getChineseZodiac(gregorianYear: number): string;
125
+ /**
126
+ * Get Chinese element for a year
127
+ * @param gregorianYear - Gregorian year
128
+ */
129
+ export declare function getChineseElement(gregorianYear: number): string;
130
+ /**
131
+ * Get full Chinese zodiac description (element + animal)
132
+ * @param gregorianYear - Gregorian year
133
+ */
134
+ export declare function getChineseZodiacFull(gregorianYear: number): string;
135
+ /**
136
+ * Convert calendar date to string representation
137
+ */
138
+ export declare function calendarDateToString(calendarDate: CalendarDate): string;
139
+ /**
140
+ * Compare two calendar dates
141
+ * @returns negative if a < b, 0 if equal, positive if a > b
142
+ */
143
+ export declare function compareCalendarDates(a: CalendarDate, b: CalendarDate): number;
144
+ /**
145
+ * Get current date in specified calendar
146
+ */
147
+ export declare function today(calendar: CalendarType): CalendarDate;
148
+ /**
149
+ * Check if two calendar dates are the same day
150
+ */
151
+ export declare function isSameCalendarDay(a: CalendarDate, b: CalendarDate): boolean;
152
+ /**
153
+ * Get supported calendar systems
154
+ */
155
+ export declare function getSupportedCalendars(): CalendarType[];
156
+ //# sourceMappingURL=calendars.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"calendars.d.ts","sourceRoot":"","sources":["../src/calendars.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,MAAM,WAAW,YAAY;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