M
M
Mirzhalol Mirkhomitov2020-12-01 10:32:56
JavaScript
Mirzhalol Mirkhomitov, 2020-12-01 10:32:56

How to highlight the current day of the week?

The site has the days of the week.
5fc5f11b5a2bd352747445.png
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

1 answer(s)
D
Dima Pautov, 2020-12-01
@Mirami97

// Создать массив дней
const days = ['Понедельник', 'Вторник'] // и т.п.

// Получить индекс дня недели
const cuttentDayIndex = new Date().getDay();

// Получить из массива название дня недели по индексу.
// Но т.к. дни недели в getDay начинаются с 1, то вторник будет 2, а раз массивы считаются с 0, то вычитаем - 1
const currentDay = days[cuttentDayIndex - 1];

For vue something like this:
<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 question

Ask a Question

731 491 924 answers to any question