Проставить даты на кнопках автоматически JS
Я создал календарь с расписанием лекций/пар, вся информация вытягивается из файлов JSON и JS сам формирует карточки в зависимости от дня недели и элемента из json файла, пример здесь.
Задача состоит в том, чтобы не вытягивать из файлов json даты, а проставлять их автоматически на кнопках. Я вынес отдельно эту задачу на Codepen, 2 день не могу понять как сделать автоматическое проставление дат. Текущую дату я могу отследить и отформатировать в виде (день недели, число, месяц, год) но только на 1 день...
Исходники Codepen тут
<nav class="schedule-navigation-panel">
<ul>
<li><button>Пн<br><span class="date-time"></span></button></li>
<li><button>Вт<br><span class="date-time"></span></button></li>
<li><button>Ср<br><span class="date-time"></span></button></li>
<li><button>Чт<br><span class="date-time"></span></button></li>
<li><button>Пт<br><span class="date-time"></span></button></li>
<li><button>Сб<br><span class="date-time"></span></button></li>
<li><button>Вс<br><span class="date-time"></span></button></li>
</ul>
<div class="week-cont">
<div class="numerator">Числитель</div>
<div class="denumerator">Знаменатель</div>
</div>
</nav>
@import "https://fonts.googleapis.com/css2?family=Oswald:wght@200;300;400&display=swap";
button {
border: none;
outline: none;
&:focus {
outline: none;
}
}
.schedule-navigation-panel {
ul {
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: space-between;
@media screen and (max-width:660px) {
li {
flex: 1 1 33%;
padding: 5px;
@media screen and (max-width:350px) {
flex: 1 1 50%;
}
@media screen and (max-width:250px) {
flex: 1 1 100%;
}
button {
width: 100%;
box-shadow: 0 0 0 1px gray;
}
}
}
li {
&.active {
button {
background-color: #1ba4ee;
color: white;
}
}
button {
position: relative;
top: 0;
width: 100%;
font-family: 'Oswald', sans-serif;
flex-basis: auto;
font-size: 1.5em;
font-weight: normal;
background-color: #f7f7f7;
cursor: pointer;
text-align: center;
padding: 5px 10px;
line-height: 1;
border-radius: 4px;
box-shadow: 0 0 0 1px lightgray, inset 0 0 0px gray, 0px 2px 20px lightgray;
transition: .15s linear;
&:active {
top: 3px;
box-shadow: 0 0 0 1px lightgray, inset 0 0 10px gray, 0px 2px 10px lightgray;
}
span {
font-size: 14px;
font-weight: 200;
}
}
}
}
.week-cont {
display: flex;
flex-direction: row;
.numerator {
margin-right: 5px;
}
.denumerator {
margin-left: 5px;
}
.numerator,
.denumerator {
margin-top: 25px;
text-align: center;
flex: 1 1 50%;
box-shadow: 0 0 0 1px lightgray;
border-radius: 4px;
padding: 10px;
&.active {
background-color: green;
color: white;
}
@media screen and (max-width:660px) {
&.numerator {
margin-right: 5px;
margin-left: 5px;
}
&.denumerator {
margin-left: 5px;
margin-right: 5px;
}
}
}
}
}
Это попытки на JS сделать отображение хотя бы текущей даты...
let nowDate = new Date(),
today = nowDate.toLocaleString('ru',{day:"numeric", month:"short", year:"numeric", weekday: "short"}).substr(0,15).replace(",",""),
buttons = document.querySelectorAll('.schedule-navigation-panel ul li button'),
li_containers = document.querySelectorAll('.schedule-navigation-panel ul li'),
dateTime = document.querySelectorAll('.date-time');
buttons.forEach((item,i)=>{
if(item.textContent.toLowerCase() == today.substr(0,2)) {
li_containers[i].classList.add('active');
dateTime[i].textContent = today.substr(3,15);
}
});
Исходники полного проекта и весь код на Github
Ответы (1 шт):
В первом приближении, это будет так:
let nowDate = new Date(),
today = fDate(nowDate),
buttons = document.querySelectorAll('.schedule-navigation-panel ul li button'),
li_containers = document.querySelectorAll('.schedule-navigation-panel ul li'),
dateTime = document.querySelectorAll('.date-time');
function fDate(date, offset = 0) {
date.setDate(date.getDate() + offset);
return date.toLocaleString('ru', {
day: "numeric",
month: "short",
year: "numeric",
weekday: "short"
}).substr(0, 16).replace(",", "")
}
buttons.forEach((item, i) => {
if (item.textContent.toLowerCase() == today.substr(0, 2)) {
li_containers[i].classList.add('active');
}
dateTime[i].textContent = fDate(new Date(), (1 + i - (new Date().getDay() || 7))).substr(3, 15);
});
@import "https://fonts.googleapis.com/css2?family=Oswald:wght@200;300;400&display=swap";
button {
border: none;
outline: none;
}
button:focus {
outline: none;
}
.schedule-navigation-panel ul {
list-style-type: none;
padding: 0;
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: space-around;
}
@media screen and (max-width: 660px) {
.schedule-navigation-panel ul li {
flex: 1 1 33%;
padding: 5px;
}
}
@media screen and (max-width: 660px) and (max-width: 350px) {
.schedule-navigation-panel ul li {
flex: 1 1 50%;
}
}
@media screen and (max-width: 660px) and (max-width: 250px) {
.schedule-navigation-panel ul li {
flex: 1 1 100%;
}
}
@media screen and (max-width: 660px) {
.schedule-navigation-panel ul li button {
width: 100%;
box-shadow: 0 0 0 1px gray;
}
}
.schedule-navigation-panel ul li.active button {
background-color: #1ba4ee;
color: white;
}
.schedule-navigation-panel ul li button {
position: relative;
top: 0;
width: 100%;
font-family: 'Oswald', sans-serif;
flex-basis: auto;
font-size: 1.5em;
font-weight: normal;
background-color: #f7f7f7;
cursor: pointer;
text-align: center;
padding: 5px 10px;
line-height: 1;
border-radius: 4px;
box-shadow: 0 0 0 1px lightgray, inset 0 0 0px gray, 0px 2px 20px lightgray;
transition: .15s linear;
}
.schedule-navigation-panel ul li button:active {
top: 3px;
box-shadow: 0 0 0 1px lightgray, inset 0 0 10px gray, 0px 2px 10px lightgray;
}
.schedule-navigation-panel ul li button span {
font-size: 14px;
font-weight: 200;
}
.schedule-navigation-panel .week-cont {
display: flex;
flex-direction: row;
}
.schedule-navigation-panel .week-cont .numerator {
margin-right: 5px;
}
.schedule-navigation-panel .week-cont .denumerator {
margin-left: 5px;
}
.schedule-navigation-panel .week-cont .numerator,
.schedule-navigation-panel .week-cont .denumerator {
margin-top: 25px;
text-align: center;
flex: 1 1 50%;
box-shadow: 0 0 0 1px lightgray;
border-radius: 4px;
padding: 10px;
}
.schedule-navigation-panel .week-cont .numerator.active,
.schedule-navigation-panel .week-cont .denumerator.active {
background-color: green;
color: white;
}
@media screen and (max-width: 660px) {
.schedule-navigation-panel .week-cont .numerator.numerator,
.schedule-navigation-panel .week-cont .denumerator.numerator {
margin-right: 5px;
margin-left: 5px;
}
.schedule-navigation-panel .week-cont .numerator.denumerator,
.schedule-navigation-panel .week-cont .denumerator.denumerator {
margin-left: 5px;
margin-right: 5px;
}
}
<nav class="schedule-navigation-panel">
<ul>
<li><button>Пн<br><span class="date-time"></span></button></li>
<li><button>Вт<br><span class="date-time"></span></button></li>
<li><button>Ср<br><span class="date-time"></span></button></li>
<li><button>Чт<br><span class="date-time"></span></button></li>
<li><button>Пт<br><span class="date-time"></span></button></li>
<li><button>Сб<br><span class="date-time"></span></button></li>
<li><button>Вс<br><span class="date-time"></span></button></li>
</ul>
<div class="week-cont">
<div class="numerator">Числитель</div>
<div class="denumerator">Знаменатель</div>
</div>
</nav>
