2023-10-23 00:21:06 +00:00
|
|
|
import { daysInMonth, yyyymmdd } from "@calcom/lib/date-fns";
|
|
|
|
|
|
|
|
// calculate the available dates in the month:
|
|
|
|
// *) Intersect with included dates.
|
|
|
|
// *) Dates in the past are not available.
|
|
|
|
// *) Use right amount of days in given month. (28, 30, 31)
|
|
|
|
export function getAvailableDatesInMonth({
|
2023-10-25 13:18:25 +00:00
|
|
|
browsingDate,
|
2023-10-23 00:21:06 +00:00
|
|
|
minDate = new Date(),
|
|
|
|
includedDates,
|
|
|
|
}: {
|
|
|
|
browsingDate: Date;
|
|
|
|
minDate?: Date;
|
|
|
|
includedDates?: string[];
|
|
|
|
}) {
|
|
|
|
const dates = [];
|
|
|
|
const lastDateOfMonth = new Date(
|
2023-10-25 13:18:25 +00:00
|
|
|
browsingDate.getFullYear(),
|
|
|
|
browsingDate.getMonth(),
|
|
|
|
daysInMonth(browsingDate)
|
2023-10-23 00:21:06 +00:00
|
|
|
);
|
|
|
|
for (
|
|
|
|
let date = browsingDate > minDate ? browsingDate : minDate;
|
|
|
|
date <= lastDateOfMonth;
|
2023-10-25 13:18:25 +00:00
|
|
|
date = new Date(date.getFullYear(), date.getMonth(), date.getDate() + 1)
|
2023-10-23 00:21:06 +00:00
|
|
|
) {
|
|
|
|
// intersect included dates
|
|
|
|
if (includedDates && !includedDates.includes(yyyymmdd(date))) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
dates.push(yyyymmdd(date));
|
|
|
|
}
|
|
|
|
return dates;
|
|
|
|
}
|