782 B
782 B
We'll create the table as a string: "<table>...</table>"
, and then assign it to innerHTML
.
The algorithm:
- Create the table header with
<th>
and weekday names. - Create the date object
d = new Date(year, month-1)
. That's the first day ofmonth
(taking into account that months in JavaScript start from0
, not1
). - First few cells till the first day of the month
d.getDay()
may be empty. Let's fill them in with<td></td>
. - Increase the day in
d
:d.setDate(d.getDate()+1)
. Ifd.getMonth()
is not yet the next month, then add the new cell<td>
to the calendar. If that's a Sunday, then add a newline"</tr><tr>"
. - If the month has finished, but the table row is not yet full, add empty
<td>
into it, to make it square.