Getting Last Business Days of the Year in TypeScript
Here's a solution that includes two functions:
getLastBusinessDaysOfYear - Gets all last business days for each month in a given year
getNBusinessDaysAfterLastBusinessDay - Gets N business days after the last business day of a specified month
/**
* Checks if a date is a business day (not weekend and not in excluded dates)
* @param date The date to check
* @param excludedDates Collection of dates to exclude (holidays, etc.)
* @returns True if it's a business day, false otherwise
*/
function isBusinessDay(date: Date, excludedDates: Date[]): boolean {
const day = date.getDay();
// Check if it's Saturday (6) or Sunday (0)
if (day === 0 || day === 6) {
return false;
}
// Check if the date is in the excluded dates
const dateStr = date.toDateString();
return !excludedDates.some(excludedDate => excludedDate.toDateString() === dateStr);
}
/**
* Gets the last business day of a given month
* @param year The year
* @param month The month (0-11)
* @param excludedDates Collection of dates to exclude
* @returns The last business day of the month
*/
function getLastBusinessDayOfMonth(year: number, month: number, excludedDates: Date[]): Date {
// Start from the last day of the month
const lastDay = new Date(year, month + 1, 0);
let currentDay = new Date(lastDay);
// Work backwards to find the last business day
while (!isBusinessDay(currentDay, excludedDates)) {
currentDay.setDate(currentDay.getDate() - 1);
}
return currentDay;
}
/**
* Gets all last business days for each month in a given year
* @param year The year to check
* @param excludedDates Collection of dates to exclude
* @returns Array of last business days for each month (index 0 = January)
*/
function getLastBusinessDaysOfYear(year: number, excludedDates: Date[] = []): Date[] {
const lastBusinessDays: Date[] = [];
for (let month = 0; month < 12; month++) {
const lastBusinessDay = getLastBusinessDayOfMonth(year, month, excludedDates);
lastBusinessDays.push(lastBusinessDay);
}
return lastBusinessDays;
}
/**
* Gets N business days after the last business day of a specified month
* @param year The year
* @param month The month (0-11)
* @param n Number of business days to add
* @param excludedDates Collection of dates to exclude
* @returns The date that is N business days after the last business day of the month
*/
function getNBusinessDaysAfterLastBusinessDay(
year: number,
month: number,
n: number,
excludedDates: Date[] = []
): Date {
// Get the last business day of the specified month
let currentDate = getLastBusinessDayOfMonth(year, month, excludedDates);
// If n is 0, just return the last business day
if (n === 0) {
return new Date(currentDate);
}
// Move forward to find the next N business days
let count = 0;
while (count < n) {
currentDate.setDate(currentDate.getDate() + 1);
if (isBusinessDay(currentDate, excludedDates)) {
count++;
}
}
return currentDate;
}
// Example usage:
const excludedDates = [
new Date(2023, 11, 25), // Christmas (example)
new Date(2023, 11, 26), // Boxing Day (example)
];
// Get all last business days of 2023
const lastBusinessDays2023 = getLastBusinessDaysOfYear(2023, excludedDates);
console.log("Last business days of 2023:");
lastBusinessDays2023.forEach((date, index) => {
console.log(`${new Date(2023, index, 1).toLocaleString('default', { month: 'long' })}: ${date.toDateString()}`);
});
// Get 5 business days after the last business day of December 2023
const fiveDaysAfter = getNBusinessDaysAfterLastBusinessDay(2023, 11, 5, excludedDates);
console.log(`5 business days after last business day of December 2023: ${fiveDaysAfter.toDateString()}`);
Explanation:
-
isBusinessDay: Helper function that checks if a date is a business day (not weekend and not in excluded dates).
-
getLastBusinessDayOfMonth: Finds the last business day of a given month by starting from the last day and working backwards.
-
getLastBusinessDaysOfYear: Uses the previous function to get the last business day for each month in a year.
-
getNBusinessDaysAfterLastBusinessDay: First finds the last business day of the specified month, then counts forward N business days from that date.
The example usage shows how to:
- Get all last business days for 2023
- Get 5 business days after the last business day of December 2023
You can customize the excludedDates array with any dates you want to exclude (like holidays). The functions handle edge cases like months ending on weekends or holidays.