2023-08-23 20:21:01 +00:00
|
|
|
import {
|
|
|
|
addDays,
|
|
|
|
endOfMonth,
|
2023-08-24 17:37:23 +00:00
|
|
|
getWeek,
|
2023-08-23 20:21:01 +00:00
|
|
|
isSameWeek,
|
|
|
|
startOfMonth,
|
2023-08-24 02:11:12 +00:00
|
|
|
startOfWeek,
|
|
|
|
subDays,
|
2023-08-23 20:21:01 +00:00
|
|
|
} from "date-fns";
|
|
|
|
|
2023-08-24 17:37:23 +00:00
|
|
|
export const daysOfWeek = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
|
|
|
|
|
2023-08-23 20:21:01 +00:00
|
|
|
/**
|
|
|
|
* Return week bounds in a [closed, open) fashion
|
|
|
|
* The last week is not returned if it contains the next month's start date
|
|
|
|
* @param date
|
|
|
|
*/
|
2023-08-24 02:11:12 +00:00
|
|
|
export function weekBoundsOfMonth(date: Date): [Date, Date] {
|
2023-08-23 20:21:01 +00:00
|
|
|
const firstDayOfMonth = startOfMonth(date);
|
2023-08-24 02:11:12 +00:00
|
|
|
const startWeek = startOfWeek(firstDayOfMonth);
|
2023-08-23 20:21:01 +00:00
|
|
|
|
|
|
|
const lastDayOfMonth = endOfMonth(date);
|
|
|
|
const firstDayOfNextMonth = addDays(lastDayOfMonth, 1);
|
2023-08-24 02:11:12 +00:00
|
|
|
let endWeek = startOfWeek(lastDayOfMonth);
|
2023-08-24 17:37:23 +00:00
|
|
|
|
2023-08-24 02:11:12 +00:00
|
|
|
if (isSameWeek(lastDayOfMonth, firstDayOfNextMonth))
|
|
|
|
endWeek = subDays(endWeek, 7);
|
2023-08-23 20:21:01 +00:00
|
|
|
|
|
|
|
return [startWeek, endWeek];
|
|
|
|
}
|
2023-08-24 02:11:12 +00:00
|
|
|
|
2023-08-24 17:37:23 +00:00
|
|
|
export function weekString(date: Date): string {
|
|
|
|
return `${getWeek(date)}/${date.getFullYear()}`;
|
|
|
|
}
|
|
|
|
|
2023-08-24 02:11:12 +00:00
|
|
|
export function monthNameOf(date: Date): string {
|
|
|
|
return date.toLocaleDateString("default", { month: "long" });
|
|
|
|
}
|