Get all last business days of the year in typescript. The business days are days that…
- User
Get all last business days of the year in typescript. The business days are days that are not in a collection of given date and not a saturday or a sunday. Then write another function that reuse this code to get N business days after the last business day of the month
Assistant
Here's a TypeScript solution for getting the last business days of the year and then N business days after the last business day of a month:
// Function to check if a date is a weekend (Saturday or Sunday) function isWeekend(date: Date): boolean { const day = date.getDay(); return day === 0 || day === 6; // 0 = Sunday, 6 = Saturday } // Function to check if a date is in the holidays collection function isHoliday(date: Date, holidays: Date[]): boolean { return holidays.some(holiday => holiday.getFullYear() === date.getFullYear() && holiday.getMonth() === date.getMonth() && holiday.getDate() === date.getDate() ); } // Function to check if a date is a business day function isBusinessDay(date: Date, holidays: Date[]): boolean { return !isWeekend(date) && !isHoliday(date, holidays); } // Function to get all last business days of each month in a year function getLastBusinessDaysOfYear(year: number, holidays: Date[] = []): Date[] { const lastBusinessDays: Date[] = []; for (let month = 0; month < 12; month++) { // Get the last day of the month const lastDayOfMonth = new Date(year, month + 1, 0); // Work backwards from the last day to find the last business day let currentDate = new Date(lastDayOfMonth); while (!isBusinessDay(currentDate, holidays)) { currentDate.setDate(currentDate.getDate() - 1); } lastBusinessDays.push(new Date(currentDate)); } return lastBusinessDays; } // Function to get N business days after the last business day of a given month function getNBusinessDaysAfterLastBusinessDayOfMonth( year: number, month: number, // 0-based (0 = January, 11 = December) n: number, holidays: Date[] = [] ): Date[] { // Get the last day of the specified month const lastDayOfMonth = new Date(year, month + 1, 0); // Find the last business day of the month let lastBusinessDay = new Date(lastDayOfMonth); while (!isBusinessDay(lastBusinessDay, holidays)) { lastBusinessDay.setDate(lastBusinessDay.getDate() - 1); } // Now find N business days after this date const businessDays: Date[] = []; let currentDate = new Date(lastBusinessDay); currentDate.setDate(currentDate.getDate() + 1); // Start from the day after while (businessDays.length < n) { if (isBusinessDay(currentDate, holidays)) { businessDays.push(new Date(currentDate)); } currentDate.setDate(currentDate.getDate() + 1); } return businessDays; } // Example usage: const holidays2024: Date[] = [ new Date(2024, 0, 1), // New Year's Day new Date(2024, 6, 4), // Independence Day new Date(2024, 11, 25), // Christmas Day // Add more holidays as needed ]; // Get all last business days of 2024 const lastBusinessDays2024 = getLastBusinessDaysOfYear(2024, holidays2024); console.log('Last business days of each month in 2024:', lastBusinessDays2024); // Get 5 business days after the last business day of March 2024 const fiveBusinessDaysAfterMarch = getNBusinessDaysAfterLastBusinessDayOfMonth( 2024, 2, // March (0-based) 5, holidays2024 ); console.log('5 business days after last business day of March 2024:', fiveBusinessDaysAfterMarch);The code includes:
- Helper functions to check weekends, holidays, and business days
getLastBusinessDaysOfYear- Returns an array of the last business day for each month in the given yeargetNBusinessDaysAfterLastBusinessDayOfMonth- Reuses the business day logic to find N business days after the last business day of a specific month
The functions handle edge cases like when the last day of a month falls on a weekend or holiday, and they properly traverse into the next month when finding subsequent business days.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.