1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
const { default: dayjs } = await import("https://esm.sh/dayjs");
export async function countWeekdaysInMonth(
weekday:
| "Monday"
| "Tuesday"
| "Wednesday"
| "Thursday"
| "Friday"
| "Saturday"
| "Sunday",
month: Date = null,
): Promise<number> {
const referenceDate = month ?? new Date();
const startOfMonth = dayjs(referenceDate).startOf("month");
const endOfMonth = dayjs(referenceDate).endOf("month");
let dateCursor = startOfMonth.clone();
let found = 0;
while (dateCursor.isBefore(endOfMonth)) {
if (dateCursor.format("dddd") === weekday) {
found++;
}
dateCursor = dateCursor.add(1, "day");
}
return found;
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
November 25, 2023