UNPKG

everyutil

Version:

A comprehensive library of lightweight, reusable utility functions for JavaScript and TypeScript, designed to streamline common programming tasks such as string manipulation, array processing, date handling, and more.

28 lines (27 loc) 827 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.dateNthWeekdayOfMonth = void 0; /** * Finds the Nth occurrence of a specific weekday in a month. * @author @dailker * @param {number} year * @param {number} month - 0=Jan, 11=Dec * @param {number} weekday - 0=Sun, 6=Sat * @param {number} n - Nth occurrence * @returns {Date|null} */ function dateNthWeekdayOfMonth(year, month, weekday, n) { let count = 0; for (let day = 1; day <= 31; day++) { const d = new Date(year, month, day); if (d.getMonth() !== month) break; if (d.getDay() === weekday) { count++; if (count === n) return d; } } return null; } exports.dateNthWeekdayOfMonth = dateNthWeekdayOfMonth;