2023-02-02 09:51:22 +00:00
|
|
|
import moment from "moment";
|
|
|
|
|
|
|
|
export const getCalendarDays = (date) => {
|
2023-02-05 10:37:30 +00:00
|
|
|
const observedDate = moment(date);
|
2023-02-02 09:51:22 +00:00
|
|
|
|
|
|
|
const prevMonthDays = [];
|
|
|
|
const currentMonthDays = [];
|
|
|
|
const nextMonthDays = [];
|
|
|
|
const maxCalendarDays = 42;
|
|
|
|
|
2023-02-05 10:37:30 +00:00
|
|
|
const firstCalendarMonday = observedDate
|
|
|
|
.clone()
|
|
|
|
.startOf("month")
|
|
|
|
.startOf("week")
|
|
|
|
.date();
|
2023-02-02 09:51:22 +00:00
|
|
|
|
2023-02-05 10:37:30 +00:00
|
|
|
for (let i = 1; i <= observedDate.clone().daysInMonth(); i++) {
|
2023-02-02 09:51:22 +00:00
|
|
|
currentMonthDays.push({
|
2023-02-05 10:37:30 +00:00
|
|
|
key: observedDate.clone().format("YYYY-MM") + "-" + i,
|
2023-02-02 09:51:22 +00:00
|
|
|
value: i,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-02-05 10:37:30 +00:00
|
|
|
if (firstCalendarMonday !== 1) {
|
|
|
|
const prevMonthLength = observedDate
|
|
|
|
.clone()
|
|
|
|
.subtract(1, "months")
|
|
|
|
.daysInMonth();
|
2023-02-02 09:51:22 +00:00
|
|
|
for (let i = firstCalendarMonday; i <= prevMonthLength; i++) {
|
|
|
|
prevMonthDays.push({
|
2023-02-05 10:37:30 +00:00
|
|
|
key:
|
|
|
|
observedDate.clone().subtract(1, "months").format("YYYY-MM") +
|
|
|
|
"-" +
|
|
|
|
i,
|
2023-02-02 09:51:22 +00:00
|
|
|
value: i,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (
|
|
|
|
let i = 1;
|
|
|
|
i <= maxCalendarDays - currentMonthDays.length - prevMonthDays.length;
|
|
|
|
i++
|
|
|
|
) {
|
|
|
|
nextMonthDays.push({
|
2023-02-05 10:37:30 +00:00
|
|
|
key: observedDate.clone().add(1, "months").format("YYYY-MM") + "-" + i,
|
2023-02-02 09:51:22 +00:00
|
|
|
value: i,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return { prevMonthDays, currentMonthDays, nextMonthDays };
|
|
|
|
};
|