A calendar app sounds simple until you build one: months have different lengths, weeks start on different days, and February changes every four years. Building one from scratch is a great beginner project because it teaches the Date object properly — and once you can render a month grid, you can build booking systems, planners, and scheduling tools.
The core idea is simple. For any month, you need three numbers: how many days it has, which weekday the 1st falls on, and which month and year you are showing. JavaScript's Date object gives you all three with a trick — day 0 of the next month is the last day of the current month:
let current = new Date();
function renderCalendar() {
const year = current.getFullYear();
const month = current.getMonth();
const daysInMonth = new Date(year, month + 1, 0).getDate();
const firstDay = new Date(year, month, 1).getDay();
document.getElementById("month-label").textContent =
current.toLocaleString("default", { month: "long", year: "numeric" });
const grid = document.getElementById("calendar-grid");
grid.innerHTML = "";
for (let i = 0; i < firstDay; i++) {
grid.appendChild(document.createElement("div"));
}
for (let day = 1; day <= daysInMonth; day++) {
const cell = document.createElement("div");
cell.className = "day";
cell.textContent = day;
const today = new Date();
if (
day === today.getDate() &&
month === today.getMonth() &&
year === today.getFullYear()
) {
cell.classList.add("today");
}
cell.addEventListener("click", () => selectDay(day));
grid.appendChild(cell);
}
}
Navigation is two buttons that move the month forward or back. Changing the month on a Date object handles year rollover for free — December plus one becomes January of the next year:
document.getElementById("prev").addEventListener("click", () => {
current.setMonth(current.getMonth() - 1);
renderCalendar();
});
document.getElementById("next").addEventListener("click", () => {
current.setMonth(current.getMonth() + 1);
renderCalendar();
});
Events make the calendar useful. Store them in an object keyed by date string, and render a dot on any day that has events. localStorage keeps them after a refresh:
const events = JSON.parse(localStorage.getItem("calendar-events") || "{}");
function addEvent(dateKey, text) {
events[dateKey] = events[dateKey] || [];
events[dateKey].push(text);
localStorage.setItem("calendar-events", JSON.stringify(events));
renderCalendar();
}
function selectDay(day) {
const key = `${current.getFullYear()}-${current.getMonth() + 1}-${day}`;
const text = prompt(`Add an event for ${key}:`);
if (text) addEvent(key, text);
}
A calendar is only half the story — the other half is how you actually use the time it shows. If you want to get better at planning your days around the events you add, the best time management books for beginners in Hindi explain simple systems like time-blocking and the 80/20 rule that pair well with a calendar you built yourself.
Ways to extend it: show event titles inside the day cells, add a week view, add drag-and-drop to move events between days, or highlight the selected day and list its events in a side panel. The Date tricks here — day 0 of the next month, getDay for the first weekday — are the same ones used in every booking and scheduling interface on the web.
Top comments (0)