CalendarProvider
@epilot/switching-deadlines / index / CalendarProvider
Class: CalendarProvider
Defined in: calendar-provider.ts:68
Calendar provider that manages holidays, working days, and date calculations.
This class provides functionality for:
- Holiday detection (fixed, moving, and special holidays)
- Working day calculations
- Date range analysis
- Custom holiday configuration
- German BundeslΓ€nder-specific holiday support
Exampleβ
const calendar = new CalendarProvider({
customCalendar: {
holidays: [
{ date: '2024-12-24', name: 'Christmas Eve', type: HolidayType.SPECIAL_HOLIDAY }
]
},
useSpecialHolidays: true
});
const isWorking = calendar.isWorkingDay('2024-12-25'); // false (Christmas)
const nextWorking = calendar.getNextWorkingDay('2024-12-25');
Constructorsβ
Constructorβ
new CalendarProvider(
options?):CalendarProvider
Defined in: calendar-provider.ts:104
Creates a new CalendarProvider instance.
Parametersβ
options?β
Optional configuration options for the calendar.
Returnsβ
CalendarProvider
Exampleβ
const provider = new CalendarProvider({
customCalendar: {
holidays: [
{
date: '2024-03-17',
name: "St. Patrick's Day",
type: HolidayType.SPECIAL_HOLIDAY,
description: 'Irish cultural celebration',
},
],
version: { version: '1.0.0', year: 2024, lastUpdated: '2024-01-01T00:00:00Z' },
},
useSpecialHolidays: true,
});
Methodsβ
addWorkingDays()β
addWorkingDays(
startDate,workingDays):Date
Defined in: calendar-provider.ts:295
Adds a specified number of working days to a date, skipping weekends and holidays.
Parametersβ
startDateβ
The starting date (Date object or ISO date string YYYY-MM-DD)
string | Date
workingDaysβ
number
Number of working days to add (must be positive)
Returnsβ
Date
A new Date object representing the result
Exampleβ
// Add 5 working days to a Friday
const result = provider.addWorkingDays('2024-07-12', 5); // Friday + 5 working days
console.log(result); // Next Friday (skipping weekend)
// Handle holidays automatically
const beforeHoliday = provider.addWorkingDays('2024-12-20', 3); // Skips Christmas
countWorkingDays()β
countWorkingDays(
startDate,endDate):number
Defined in: calendar-provider.ts:404
Counts the number of working days between two dates (inclusive).
Parametersβ
startDateβ
The start date of the range (Date object or ISO date string YYYY-MM-DD)
string | Date
endDateβ
The end date of the range (Date object or ISO date string YYYY-MM-DD)
string | Date
Returnsβ
number
The number of working days in the specified range
Exampleβ
const workingDaysCount = provider.countWorkingDays('2024-07-01', '2024-07-31');
console.log(`There are ${workingDaysCount} working days in July 2024`);
// Quick calculation for project planning
const projectDays = provider.countWorkingDays('2024-01-15', '2024-03-15');
getCalendarVersion()β
getCalendarVersion():
CalendarVersion
Defined in: calendar-provider.ts:477
Gets the current calendar version information.
Returnsβ
The CalendarVersion object containing version, year, and last updated information
Exampleβ
const version = provider.getCalendarVersion();
console.log(`Calendar version: ${version.version} for year ${version.year}`);
console.log(`Last updated: ${version.lastUpdated}`);
getDayInfo()β
getDayInfo(
date):DayInfo
Defined in: calendar-provider.ts:252
Gets detailed information about a specific day including working day status and holiday information.
Parametersβ
dateβ
The date to analyze (Date object or ISO date string YYYY-MM-DD)
string | Date
Returnsβ
Detailed day information including date, working day status, and holiday details
Exampleβ
const dayInfo = provider.getDayInfo('2024-12-25');
console.log(dayInfo);
// {
// date: '2024-12-25',
// isWorkingDay: false,
// holiday: { date: '2024-12-25', name: 'Weihnachtstag', type: 'PUBLIC_HOLIDAY' }
// }
getNextWorkingDay()β
getNextWorkingDay(
date):Date
Defined in: calendar-provider.ts:427
Gets the next working day from a given date.
Parametersβ
dateβ
The reference date (Date object or ISO date string YYYY-MM-DD)
string | Date
Returnsβ
Date
A Date object representing the next working day
Exampleβ
// If today is Friday, get next Monday (or Tuesday if Monday is a holiday)
const nextWorking = provider.getNextWorkingDay('2024-07-12'); // Friday
console.log(nextWorking); // Monday July 15, 2024 (or next working day)
// Automatically skips holidays
const afterChristmas = provider.getNextWorkingDay('2024-12-25');
getNonWorkingDaysInRange()β
getNonWorkingDaysInRange(
startDate,endDate):DayInfo[]
Defined in: calendar-provider.ts:368
Gets all non-working days (weekends and holidays) between two dates (inclusive).
Parametersβ
startDateβ
The start date of the range (Date object or ISO date string YYYY-MM-DD)
string | Date
endDateβ
The end date of the range (Date object or ISO date string YYYY-MM-DD)
string | Date
Returnsβ
DayInfo[]
Array of DayInfo objects for all non-working days in the range
Exampleβ
const nonWorkingDays = provider.getNonWorkingDaysInRange('2024-12-01', '2024-12-31');
console.log(`${nonWorkingDays.length} holidays and weekends in December 2024`);
nonWorkingDays.forEach(day => {
if (day.holiday) {
console.log(`${day.date}: ${day.holiday.name} (${day.holiday.type})`);
}
});
getPreviousWorkingDay()β
getPreviousWorkingDay(
date):Date
Defined in: calendar-provider.ts:454
Gets the previous working day from a given date.
Parametersβ
dateβ
The reference date (Date object or ISO date string YYYY-MM-DD)
string | Date
Returnsβ
Date
A Date object representing the previous working day
Exampleβ
// If today is Monday, get previous Friday (or earlier if Friday was a holiday)
const prevWorking = provider.getPreviousWorkingDay('2024-07-15'); // Monday
console.log(prevWorking); // Friday July 12, 2024 (or previous working day)
// Automatically skips holidays
const beforeNewYear = provider.getPreviousWorkingDay('2024-01-02');
getWorkingDaysInRange()β
getWorkingDaysInRange(
startDate,endDate):DayInfo[]
Defined in: calendar-provider.ts:329
Gets all working days between two dates (inclusive).
Parametersβ
startDateβ
The start date of the range (Date object or ISO date string YYYY-MM-DD)
string | Date
endDateβ
The end date of the range (Date object or ISO date string YYYY-MM-DD)
string | Date
Returnsβ
DayInfo[]
Array of DayInfo objects for all working days in the range
Exampleβ
const workingDays = provider.getWorkingDaysInRange('2024-07-01', '2024-07-31');
console.log(`${workingDays.length} working days in July 2024`);
workingDays.forEach(day => {
console.log(`${day.date} is a working day`);
});
isHoliday()β
isHoliday(
date):false|Holiday
Defined in: calendar-provider.ts:195
Checks if a specific date is a holiday.
Parametersβ
dateβ
The date to check (Date object or ISO date string YYYY-MM-DD)
string | Date
Returnsβ
false | Holiday
The Holiday object if the date is a holiday, false otherwise
Exampleβ
const holiday = provider.isHoliday('2024-12-25');
if (holiday) {
console.log(`${holiday.name} is a ${holiday.type}`);
}
const dateHoliday = provider.isHoliday(new Date('2024-01-01'));
isWorkingDay()β
isWorkingDay(
date):boolean
Defined in: calendar-provider.ts:219
Checks if a specific date is a working day (not a weekend or holiday).
Parametersβ
dateβ
The date to check (Date object or ISO date string YYYY-MM-DD)
string | Date
Returnsβ
boolean
True if the date is a working day, false if it's a weekend or holiday
Exampleβ
const isWorking = provider.isWorkingDay('2024-07-15'); // true (Monday, no holiday)
const isWeekend = provider.isWorkingDay('2024-07-14'); // false (Sunday)
const isHoliday = provider.isWorkingDay('2024-12-25'); // false (Christmas)
updateCustomHolidays()β
updateCustomHolidays(
customHolidays):void
Defined in: calendar-provider.ts:504
Updates the custom holidays configuration and clears the holiday cache.
Parametersβ
customHolidaysβ
Array of custom holiday configurations to replace existing ones
Returnsβ
void
Exampleβ
provider.updateCustomHolidays([
{
date: '2024-04-01',
name: 'Company Foundation Day',
type: HolidayType.SPECIAL_HOLIDAY,
description: 'Annual company celebration'
},
{
date: '2024-07-04',
name: 'Independence Day',
type: HolidayType.PUBLIC_HOLIDAY,
bundeslaender: ['BY'] // Bavaria only
}
]);