V
V
Vitaly2021-01-30 22:35:27
JavaScript
Vitaly, 2021-01-30 22:35:27

How to filter items by day?

You need to filter by an array with objects for a week, day, month.
The object stores a specific day.

Data format:

"test": [
    {
      "id": "uVB6FJQUH",
      "type": "kino",
      "total": 70,
      "date": "17.01.2021",

    }]


I did filtering by day, I can’t figure out how to do it in a week, as far as I understand, you first need to create an interval of days for the WEEK button (Calendar week) and compare whether my object is included in it, tell me how to implement it, I’m doing it on the front.

6015b2572ad9a366970042.jpeg

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Valery, 2021-02-26
@vmakhnyuk

you can use the date-fns library . Namely, the methods:

  • subBusinessDays
  • isWithinInterval

Implementation
const lastBusinessWeekStart = subBusinessDays(new Date(), 7);
const lastBusinessMonthStart = subBusinessDays(new Date(), 30);
// фильтр неделя:
test.filter(({ date }) => isWithinInterval(
  new Date(date),
  { start: lastBusinessWeekStart, end: new Date() }
));

//фильтр месяц:
test.filter(({ date }) => isWithinInterval(
  new Date(date),
  { start: lastBusinessMonthStart, end: new Date() }
));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question