ts-time-utils 3.0.4 → 4.1.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 +186 -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/finance.d.ts +236 -0
- package/dist/esm/finance.d.ts.map +1 -0
- package/dist/esm/finance.js +495 -0
- package/dist/esm/healthcare.d.ts +260 -0
- package/dist/esm/healthcare.d.ts.map +1 -0
- package/dist/esm/healthcare.js +447 -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 +19 -7
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +23 -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/scheduling.d.ts +206 -0
- package/dist/esm/scheduling.d.ts.map +1 -0
- package/dist/esm/scheduling.js +329 -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/finance.d.ts +236 -0
- package/dist/finance.d.ts.map +1 -0
- package/dist/finance.js +495 -0
- package/dist/healthcare.d.ts +260 -0
- package/dist/healthcare.d.ts.map +1 -0
- package/dist/healthcare.js +447 -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 +19 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +23 -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/scheduling.d.ts +206 -0
- package/dist/scheduling.d.ts.map +1 -0
- package/dist/scheduling.js +329 -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 +31 -1
|
@@ -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;IAC3B,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;IACd,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED,MAAM,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M,CAIvE;AAED;;;GAGG;AACH,wBAAgB,oBAAoB,CAAC,CAAC,EAAE,YAAY,EAAE,CAAC,EAAE,YAAY,GAAG,MAAM,CAQ7E;AAED;;GAEG;AACH,wBAAgB,KAAK,CAAC,QAAQ,EAAE,YAAY,GAAG,YAAY,CAE1D;AAED;;GAEG;AACH,wBAAgB,iBAAiB,CAAC,CAAC,EAAE,YAAY,EAAE,CAAC,EAAE,YAAY,GAAG,OAAO,CAK3E;AAED;;GAEG;AACH,wBAAgB,qBAAqB,IAAI,YAAY,EAAE,CAWtD"}
|
|
@@ -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,GAAG,SAAS,CAY9E;AAED;;;;;;;GAOG;AACH,wBAAgB,WAAW,CACzB,KAAK,EAAE,IAAI,EAAE,EACb,SAAS,GAAE,IAAI,GAAG,QAAQ,GAAG,QAAQ,GAAG,MAAM,GAAG,KAAY,GAC5D,IAAI,EAAE,CAaR;AAoBD;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
|
|
|
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,CA4BnG;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,GAAG,EAAE,IAAI,EAAE,SAAS,GAAE,OAAc,GAAG,OAAO,CAShG;AAED;;;;GAIG;AACH,wBAAgB,mB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