<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" href="http://cactus.lark.ru/Kalendar/css/kalendar.css">
</head>
<body>
<div id="num_year">
Календарь на
<button class="year_minus">◀</button>
<input id="year_input" type="number" size="4" value="" />
<button class="year_plus">▶</button>
год.<br />
<button id="presently">сегодня</button>
</div>
<table id="table">
<tr>
<td class="td_month">
<div id="January"></div>
<table class="table_month"></table>
</td>
<td class="td_month">
<div id="February"></div>
<table class="table_month"></table>
</td>
<td class="td_month">
<div id="March"></div>
<table class="table_month"></table>
</td>
</tr>
<tr>
<td class="td_month">
<div id="April"></div>
<table class="table_month"></table>
</td>
<td class="td_month">
<div id="May"></div>
<table class="table_month"></table>
</td>
<td class="td_month">
<div id="June"></div>
<table class="table_month"></table>
</td>
</tr>
<tr>
<td class="td_month">
<div id="July"></div>
<table class="table_month"></table>
</td>
<td class="td_month">
<div id="August"></div>
<table class="table_month"></table>
</td>
<td class="td_month">
<div id="September"></div>
<table class="table_month"></table>
</td>
</tr>
<tr>
<td class="td_month">
<div id="October"></div>
<table class="table_month"></table>
</td>
<td class="td_month">
<div id="November"></div>
<table class="table_month"></table>
</td>
<td class="td_month">
<div id="December"></div>
<table class="table_month"></table>
</td>
</tr>
</table>
<script>
var calendar = {
update: function(year, month) {
this.year = year;
this.month = ['Январь', 'Февраль', 'Март', 'Апрель', 'Май', 'Июнь', 'Июль', 'Август', 'Сентябрь', 'Октябрь', 'Ноябрь', 'Декабрь'][month];
var startDay = new Date(year, month, 1);
var offsetDay = (startDay.getDay() || 7) - 1;
startDay.setDate(startDay.getDate() - offsetDay);
var lastDay = new Date(startDay);
var days = new Date(year, month + 1, 0).getDate();
days = Math.ceil((days + offsetDay) / 7) * 7;
lastDay.setDate(lastDay.getDate() + days);
this.data = ['Пн', 'Вт', 'Ср', 'Чт', 'Пт', 'Сб', 'Вс'];
while (startDay < lastDay) {
this.data.push(startDay.getDate());
startDay.setHours(24);
}
},
getCurDay: function(month, m, d, flag = false, flag2 = false) {
var curDay;
for (var i = 0; i < this.data.length; i++) {
if (m === month) {
if ((this.data[i] === 1 && flag) || flag2) {
flag2 = true;
continue;
}
if (this.data[i] === 1 || flag) {
if (this.data[i] === d) curDay = i % 7;
flag = true;
continue;
}
}
}
return curDay;
},
render: function(month, name) {
var html = ['<tr>', '<tr>', '<tr>', '<tr>', '<tr>', '<tr>', '<tr>'];
var m = new Date().getMonth(),
d = new Date().getDate(),
flag = false,
flag2 = false,
curDay = this.getCurDay(month, m, d),
className;
for (var i = 0; i < this.data.length; i++) {
if (i >= 0 && i <= 6) {
if (i === curDay) html[i % 7] += '<td class="week-day curDay">' + this.data[i];
else html[i % 7] += '<td class="week-day">' + this.data[i];
continue;
}
if ((this.data[i] === 1 && flag) || flag2) {
flag2 = true;
className = "prevMonth";
html[i % 7] += '<td class="' + className + '">' + this.data[i];
continue;
}
if (this.data[i] === 1 || flag) {
className = "curMonth";
if (this.data[i] === d && m === month) {
className += " curDay";
document.querySelector(`#${name}`).classList.add('cur_month_name');
}
flag = true;
html[i % 7] += '<td class="' + className + '">' + this.data[i];
continue;
} else {
className = "prevMonth";
html[i % 7] += '<td class="' + className + '">' + this.data[i];
continue;
}
html[i % 7] += '<td class="prevMonth">' + this.data[i];
}
this.monthName.textContent = this.month;
this.element.innerHTML = html.join('');
}
};
var today = new Date(),
thisYear = today.getFullYear(),
thisMonth = today.getMonth(),
months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
year_minus = document.querySelector('.year_minus'),
year_plus = document.querySelector('.year_plus'),
buttonToday = document.querySelector('#presently'),
yearListener = document.querySelector('#year_input');
yearListener.value = thisYear;
function make(year = thisYear) {
months.forEach((v, i) => {
calendar.monthName = document.querySelector(`#${v}`);
calendar.element = document.querySelectorAll('.table_month')[i];
calendar.update(year, i);
calendar.render(i, months[i]);
});
};
make();
buttonToday.classList.add('hide');
year_plus.addEventListener('click', function() {
yearListener.value = ++thisYear;
if (yearListener.value == today.getFullYear()) buttonToday.classList.add('hide')
else buttonToday.classList.remove('hide');
make(thisYear);
});
year_minus.addEventListener('click', function() {
yearListener.value = --thisYear;
if (yearListener.value == today.getFullYear()) buttonToday.classList.add('hide')
else buttonToday.classList.remove('hide');
make(thisYear);
});
buttonToday.addEventListener('click', function() {
thisYear = today.getFullYear();
yearListener.value = thisYear;
make(thisYear);
buttonToday.classList.add('hide');
});
yearListener.addEventListener('change', function() {
thisYear = yearListener.value;
if (yearListener.value == today.getFullYear()) buttonToday.classList.add('hide')
else buttonToday.classList.remove('hide');
make(+yearListener.value);
});
const getMidnigth = () => Math.floor(new Date().setHours(24, 0, 0, 0) - new Date().getTime());
let timer = setTimeout(function midUpdate() {
console.log("r = " + new Date().getTime());
buttonToday.classList.add('hide');
thisYear = today.getFullYear();
yearListener.value = thisYear;
make(thisYear);
timer = setTimeout(midUpdate, getMidnigth());
},getMidnigth());
</script>
</body>
</html>