Created
June 19, 2020 12:26
-
-
Save b-aleksei/38135ff900a63f34834e9feb578d1e2c to your computer and use it in GitHub Desktop.
Calendar
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// <div id="calendar"></div> | |
function createCalendar(elem, year, month) { | |
let mon = month - 1; // месяцы в JS идут от 0 до 11, а не от 1 до 12 | |
let d = new Date(year, mon); | |
let table = '<table><tr><th>пн</th><th>вт</th><th>ср</th><th>чт</th><th>пт</th><th>сб</th><th>вс</th></tr><tr>'; | |
// пробелы для первого ряда | |
// с понедельника до первого дня месяца | |
// * * * 1 2 3 4 | |
for (let i = 1; i < d.getDay(); i++) { | |
table += '<td></td>'; | |
} | |
// <td> ячейки календаря с датами | |
while (d.getMonth() === mon) { | |
table += '<td>' + d.getDate() + '</td>'; | |
if (d.getDay() % 7 === 0) { // 0-вс, последний день - перевод строки | |
table += '</tr><tr>'; | |
} | |
d.setDate(d.getDate() + 1); | |
} | |
// добить таблицу пустыми ячейками, если нужно | |
// 29 30 31 * * * * | |
if (d.getDay() !== 0) { | |
for (let i = d.getDay(); i < 7; i++) { | |
table += '<td></td>'; | |
} | |
} | |
// закрыть таблицу | |
table += '</tr></table>'; | |
elem.innerHTML = table; | |
} | |
createCalendar(calendar, 2020, 7); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment