const MONTH_DAY_PATTERN = /^(\d{2})-(\d{2})$/; // Days per month of a non-leap year: 02-29 is not a valid reference day, so // the reference day exists in every year. const DAYS_IN_MONTH = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; /** MM-TT with a real month and a day that exists in a non-leap year. */ export function isValidMonthDay(value: string): boolean { const match = MONTH_DAY_PATTERN.exec(value); if (!match) { return false; } const month = Number(match[1]); const day = Number(match[2]); return ( month >= 1 && month <= 12 && day >= 1 && day <= DAYS_IN_MONTH[month - 1] ); } /** The reference day (YYYY-MM-DD) of a year for a stored MM-TT. */ export function tskReferenceDayOf(year: number, monthDay: string): string { if (!isValidMonthDay(monthDay)) { throw new Error(`Invalid month-day: ${monthDay}`); } return `${String(year).padStart(4, '0')}-${monthDay}`; }