UNPKG

@technobuddha/library

Version:
35 lines (34 loc) 1.93 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getOccurrenceInMonth = void 0; var constants_1 = require("../constants"); var getBeginningOfMonth_1 = __importDefault(require("../getBeginningOfMonth")); var getDaysInMonth_1 = __importDefault(require("../getDaysInMonth")); var addTime_1 = __importDefault(require("../addTime")); var modulo_1 = __importDefault(require("../modulo")); /** * Determine the date of an occurrence of a weekday within a month * * @param input A date within the month in question * @param dayOfWeek The day of the week to find the occurrence * @param occurrence The occurrence number, or 'last' to find the last occurrence * @param __namedParameters see {@link Options} * @defaultValue UTC false * @returns A date object corresponding to the occurrence requested, or null if no such date exists in the month */ function getOccurrenceInMonth(input, dayOfWeek, occurrence, _a) { var _b = _a === void 0 ? {} : _a, _c = _b.UTC, UTC = _c === void 0 ? false : _c; var day = getBeginningOfMonth_1.default(input, { UTC: UTC }); var jump = modulo_1.default(dayOfWeek - (UTC ? day.getUTCDay() : day.getDay()), constants_1.daysPerWeek); if (occurrence === 'last') return addTime_1.default(day, { days: jump + Math.floor((getDaysInMonth_1.default(input, { UTC: UTC }) - jump - 1) / constants_1.daysPerWeek) * constants_1.daysPerWeek }); else if (occurrence < 1 || occurrence > 5) return null; day = addTime_1.default(day, { days: jump + constants_1.daysPerWeek * (occurrence - 1) }); return (UTC ? day.getUTCMonth() === input.getUTCMonth() : day.getMonth() === input.getMonth()) ? day : null; } exports.getOccurrenceInMonth = getOccurrenceInMonth; exports.default = getOccurrenceInMonth;