Skip to content
doubtly

2 answers

<div class="month">

  <ul>

    <li class="prev">❮</li>

    <li class="next">❯</li>

    <li>August<br><span style="font-size:18px">2017</span></li>

  </ul>

</div>

  

<ul class="weekdays">

  <li>Mo</li>

  <li>Tu</li>

  <li>We</li>

  <li>Th</li>

  <li>Fr</li>

  <li>Sa</li>

  <li>Su</li>

</ul>

  

<ul class="days">

  <li>1</li>

  <li>2</li>

  <li>3</li>

  <li>4</li>

  <li>5</li>

  <li>6</li>

  <li>7</li>

  <li>8</li>

  <li>9</li>

  <li><span class="active">10</span></li>

  <li>11</li>

  </ul>

codelikepro · 19 Aug 2022 Report
Was this answer helpful?

A dynamic calendar is built with JavaScript, which works out the days of the current month and writes them into an HTML table.

<h3 id="title"></h3>
<table border="1" id="cal"></table>

<script>
const today = new Date();
const y = today.getFullYear(), m = today.getMonth();
const names = ["January","February","March","April","May","June",
  "July","August","September","October","November","December"];
document.getElementById("title").textContent = names[m] + " " + y;

const firstDay = new Date(y, m, 1).getDay();      // 0 = Sunday
const daysInMonth = new Date(y, m + 1, 0).getDate();

let html = "<tr><th>Su</th><th>Mo</th><th>Tu</th><th>We</th>"
         + "<th>Th</th><th>Fr</th><th>Sa</th></tr><tr>";
for (let i = 0; i < firstDay; i++) html += "<td></td>";
for (let d = 1; d <= daysInMonth; d++) {
  const mark = d === today.getDate() ? " style='background:yellow'" : "";
  html += "<td" + mark + ">" + d + "</td>";
  if ((firstDay + d) % 7 === 0) html += "</tr><tr>";
}
document.getElementById("cal").innerHTML = html + "</tr>";
</script>

new Date(y, m, 1).getDay() gives the weekday of the 1st, so that many empty cells are added first. new Date(y, m + 1, 0).getDate() gives the number of days in the month. A new row starts after every Saturday, and today's date is highlighted. Because the dates are calculated, the calendar is always correct for the current month; prev/next buttons can change m and redraw it.

Doubtly AI · 24 Oct 2026 AI-verified · Oct 2026 Report
Was this answer helpful?

Your answer

Log in or create a free account to answer this question.