logseq-calendar/lib/month.ts

31 lines
805 B
TypeScript
Raw Normal View History

2023-08-23 20:21:01 +00:00
import {
addDays,
endOfMonth,
isSameWeek,
startOfMonth,
2023-08-24 02:11:12 +00:00
startOfWeek,
subDays,
2023-08-23 20:21:01 +00:00
} 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
*/
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);
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
export function monthNameOf(date: Date): string {
return date.toLocaleDateString("default", { month: "long" });
}