Skip to content

Instantly share code, notes, and snippets.

@jenil94
Last active July 25, 2020 16:22
Show Gist options
  • Save jenil94/3610d92b230384a90ce530a446f6f4be to your computer and use it in GitHub Desktop.
Save jenil94/3610d92b230384a90ce530a446f6f4be to your computer and use it in GitHub Desktop.
....
import {
format,
startOfWeek,
addDays,
startOfMonth,
endOfMonth,
endOfWeek,
isSameMonth,
isSameDay
} from "date-fns";
const Calendar = () => {
....
const generateDatesForCurrentWeek = (date, selectedDate, activeDate) => {
let currentDate = date;
const week = [];
for (let day = 0; day < 7; day++) {
const cloneDate = currentDate;
week.push(
<div
className={`day ${
isSameMonth(currentDate, activeDate) ? "" : "inactiveDay"
} ${isSameDay(currentDate, selectedDate) ? "selectedDay" : ""}
${isSameDay(currentDate, new Date()) ? "today" : ""}`}
onClick={() => {
setSelectedDate(cloneDate);
}}
>
{format(currentDate, "d")}
</div>
);
currentDate = addDays(currentDate, 1);
}
return <>{week}</>;
};
const getDates = () => {
const startOfTheSelectedMonth = startOfMonth(activeDate);
const endOfTheSelectedMonth = endOfMonth(activeDate);
const startDate = startOfWeek(startOfTheSelectedMonth);
const endDate = endOfWeek(endOfTheSelectedMonth);
let currentDate = startDate;
const allWeeks = [];
while (currentDate <= endDate) {
allWeeks.push(
generateDatesForCurrentWeek(currentDate, selectedDate, activeDate)
);
currentDate = addDays(currentDate, 7);
}
return <div className="weekContainer">{allWeeks}</div>;
};
....
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment