35 lines
1.1 KiB
TypeScript
35 lines
1.1 KiB
TypeScript
export function addDays(date: Date, days: number): Date {
|
|
const copy = new Date(date);
|
|
copy.setHours(0, 0, 0, 0);
|
|
copy.setDate(copy.getDate() + days);
|
|
return copy;
|
|
}
|
|
|
|
export function formatYmd(date: Date): string {
|
|
return `${date.getFullYear()}${String(date.getMonth() + 1).padStart(2, "0")}${String(date.getDate()).padStart(2, "0")}`;
|
|
}
|
|
|
|
export function formatIsoDate(date: Date): string {
|
|
return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, "0")}-${String(date.getDate()).padStart(2, "0")}`;
|
|
}
|
|
|
|
export function formatRuDate(date: Date): string {
|
|
return `${String(date.getDate()).padStart(2, "0")}.${String(date.getMonth() + 1).padStart(2, "0")}.${date.getFullYear()}`;
|
|
}
|
|
|
|
export function mondayOfWeek(date: Date): Date {
|
|
const copy = new Date(date);
|
|
copy.setHours(0, 0, 0, 0);
|
|
const mondayBasedDay = (copy.getDay() + 6) % 7;
|
|
copy.setDate(copy.getDate() - mondayBasedDay);
|
|
return copy;
|
|
}
|
|
|
|
export function sundayOfWeek(date: Date): Date {
|
|
return addDays(mondayOfWeek(date), 6);
|
|
}
|
|
|
|
export function formatRuWeekRange(date: Date): string {
|
|
return `${formatRuDate(mondayOfWeek(date))} - ${formatRuDate(sundayOfWeek(date))}`;
|
|
}
|