score:1

Accepted answer

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

score:1

 date.prototype.getweek = function() {
    var date = new date(this.gettime());
    date.sethours(0, 0, 0, 0);

    // thursday in current week decides the year.
    date.setdate(date.getdate() + 3 - (date.getday() + 6) % 7);

    // january 4 is always in week 1.
    var week1 = new date(date.getfullyear(), 0, 4);

    // adjust to thursday in week 1 and count number of weeks from date to week1.
    return 1 + math.round(((date.gettime() - week1.gettime()) / 86400000 - 3 + (week1.getday() + 6) % 7) / 7);
}

score:1

you could:

  • first, get week of year by .week()
  • second, get year by .year()
  • finally, filter the date that has the same year and the same week in year with the current date

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",
  },
];

const sameyear = (date1, date2) =>
  moment(date1).year() === moment(date2).year();

const sameweek = (date1, date2) =>
  moment(date1).week() === moment(date2).week();

const sameweekinsameyear = (date1, date2) =>
  sameyear(date1, date2) && sameweek(date1, date2);

const res = notificationitems.filter(({ date }) =>
  sameweekinsameyear(date, new date())
);

console.log(res);
<script src="https://momentjs.com/downloads/moment.min.js"></script>

score:1

you have to create function with return number of week based on day number of first january and then use filter

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',

    }
  ]
  
  const getweek = date => {
    let now = new date(date);
    let firstjanuary = new date(now.getfullyear(), 0, 1);
    return math.ceil( (((now.gettime() - firstjanuary.gettime()) / 86400000) + firstjanuary.getday() + 1) / 7 );
  }
  
  
  const currentweek = getweek(new date);


  const currentweeknotificatoinitems = notificationitems.filter(item => 
      getweek(new date(item.date)) === currentweek
  )
  
 console.log(currentweeknotificatoinitems)

score:1

try using

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'
    }
];

let curr = new date; // get current date
let first = curr.getdate() - curr.getday();
let last = first + 6; 

let firstday = new date(curr.setdate(first));
let lastday = new date(curr.setdate(last));

let a = notificationitems.filter((item) => {
   let date = new date(item.date);
   if(date.gettime() >= firstday.gettime() && date.gettime() <= lastday.gettime()) {
      return item;
   }
});

console.log(a);

hope this helps :)


Related Query

More Query from same tag