Files
kita-planer/src/lib/date-utils.ts
T

59 lines
1.6 KiB
TypeScript

import {
addMonths,
getDate,
getYear,
getMonth,
format,
} from "date-fns";
import { de } from "date-fns/locale";
/**
* Der Notdienst-Plan wird immer für den nächsten Monat erstellt.
* Nach dem 26. des aktuellen Monats ist der Plan gesperrt.
*/
const LOCK_DAY = 26;
export function getTargetMonthData(now = new Date()) {
const currentDay = getDate(now);
const nextMonthDate = addMonths(now, 1);
const targetYear = getYear(nextMonthDate);
const targetMonth = getMonth(nextMonthDate) + 1; // 1-12
// Sperre aktiv, sobald der aktuelle Tag (im aktuellen Monat) > LOCK_DAY ist.
const isLocked = currentDay > LOCK_DAY;
return {
targetYear,
targetMonth,
isLocked,
monthName: format(nextMonthDate, "MMMM yyyy", { locale: de }),
};
}
export function getWorkingDaysOfMonth(year: number, month: number) {
const start = new Date(Date.UTC(year, month - 1, 1));
const end = new Date(Date.UTC(year, month, 0)); // last day of month
const days: Date[] = [];
const current = new Date(start);
while (current <= end) {
const dayOfWeek = current.getUTCDay(); // 0 is Sunday, 6 is Saturday
if (dayOfWeek !== 0 && dayOfWeek !== 6) {
days.push(new Date(current));
}
current.setUTCDate(current.getUTCDate() + 1);
}
return days;
}
export function formatDateKey(date: Date) {
return format(date, "yyyy-MM-dd");
}
export function formatUtcDateKey(date: Date) {
const yyyy = date.getUTCFullYear();
const mm = String(date.getUTCMonth() + 1).padStart(2, "0");
const dd = String(date.getUTCDate()).padStart(2, "0");
return `${yyyy}-${mm}-${dd}`;
}