* Date add 1 day adds 24 hours, not 1 day, causing the last date to be lost on dst change * Alternate fix with tests * Extract logic so test file doesnt register tsx
33 lines
967 B
TypeScript
33 lines
967 B
TypeScript
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({
|
|
browsingDate, // pass as UTC
|
|
minDate = new Date(),
|
|
includedDates,
|
|
}: {
|
|
browsingDate: Date;
|
|
minDate?: Date;
|
|
includedDates?: string[];
|
|
}) {
|
|
const dates = [];
|
|
const lastDateOfMonth = new Date(
|
|
Date.UTC(browsingDate.getFullYear(), browsingDate.getMonth(), daysInMonth(browsingDate))
|
|
);
|
|
for (
|
|
let date = browsingDate > minDate ? browsingDate : minDate;
|
|
date <= lastDateOfMonth;
|
|
date = new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate() + 1))
|
|
) {
|
|
// intersect included dates
|
|
if (includedDates && !includedDates.includes(yyyymmdd(date))) {
|
|
continue;
|
|
}
|
|
dates.push(yyyymmdd(date));
|
|
}
|
|
return dates;
|
|
}
|