Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Current week from given array of objects date in javascript

I am having array of objects with date key,i want to filter out the objects which has current week,how can i achieve this which returns me the current week of objects.i tried using filter but first i need to format date key i guess.

const notificationItems = [
    {
      id: 1,
      date: '2021-02-10T05:04:59.525589Z',


      name: 'Glenn',

    },
    {
      id: 2,
      date: '2021-02-10T05:04:59.525589Z',


      name: 'Root',

    },
    {
      id: 3,
      date: '2021-01-08T05:04:59.525589Z',


      name: 'Smith',

    },
    {
      id: 4,
      date: '2019-02-03T05:04:59.525589Z',


      name: 'Ryan',

    },
    {
      id: 5,
      date: '2019-01-03T05:04:59.525589Z',


      name: 'Bob',

    }
  ]
like image 894
KnightCrawler Avatar asked Jun 12 '26 04:06

KnightCrawler


1 Answers

Maybe you can consider using of moment.js library https://github.com/moment/moment

notificationItems.filter(
  (notification) =>
    moment(notification.date).week() === moment().week() &&
    moment(notification.date).year() === moment().year()
);

then by this code you will get only the notifications on this week

like image 104
Mohammed Said Elattar Avatar answered Jun 14 '26 19:06

Mohammed Said Elattar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!