Answer the question
In order to leave comments, you need to log in
How to highlight the current day of the week?
The site has the days of the week.
It is necessary to make the active day of the week. For example, if today is Monday, for example, red should be Monday. If it's Tuesday then it must be Tuesday, and so on.
Answer the question
In order to leave comments, you need to log in
// Создать массив дней
const days = ['Понедельник', 'Вторник'] // и т.п.
// Получить индекс дня недели
const cuttentDayIndex = new Date().getDay();
// Получить из массива название дня недели по индексу.
// Но т.к. дни недели в getDay начинаются с 1, то вторник будет 2, а раз массивы считаются с 0, то вычитаем - 1
const currentDay = days[cuttentDayIndex - 1];
<template>
<ul>
<li :class="{ active: currentDay === 'Понедельник'}">Понедельник</li>
<li :class="{ active: currentDay === 'Вторник'}">Вторник</li>
</ul>
</template>
<script>
data: () => ({
days: ['Понедельник', 'Вторник'] // и т.п.
}),
computed: {
currentDay () {
const cuttentDayIndex = new Date().getDay();
return this.days[cuttentDayIndex - 1]
}
}
</script>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question