logseq-calendar/lib/month.ts

30 lines
805 B
TypeScript

import {
addDays,
endOfMonth,
isSameWeek,
startOfMonth,
startOfWeek,
subDays,
} from "date-fns";
/**
* 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
*/
export function weekBoundsOfMonth(date: Date): [Date, Date] {
const firstDayOfMonth = startOfMonth(date);
const startWeek = startOfWeek(firstDayOfMonth);
const lastDayOfMonth = endOfMonth(date);
const firstDayOfNextMonth = addDays(lastDayOfMonth, 1);
let endWeek = startOfWeek(lastDayOfMonth);
if (isSameWeek(lastDayOfMonth, firstDayOfNextMonth))
endWeek = subDays(endWeek, 7);
return [startWeek, endWeek];
}
export function monthNameOf(date: Date): string {
return date.toLocaleDateString("default", { month: "long" });
}