it-tools/src/tools/week-number-converter/week-number-converter.service.ts
2024-10-26 10:37:20 +02:00

16 lines
635 B
TypeScript

// Returns the first day (Monday) of the specified week
// Year defaults to the current local calendar year
export function getFirstMondayFromISOWeek(weekInYear: number, year = new Date().getFullYear()) {
const d = new Date(year, 0, 4);
d.setDate(d.getDate() - (d.getDay() || 7) + 1 + 7 * (weekInYear - 1));
return d;
}
export function getFirstMondayFromMonthWeek(weekInMonth: number, month = new Date().getMonth() + 1, year = new Date().getFullYear()) {
const d = new Date(year, month - 1, 4);
const day = d.getDay() || 7;
d.setDate(d.getDate() - day + 1);
d.setDate(d.getDate() + 7 * (weekInMonth - 1));
return d;
}