如何从 javascript 中的给定对象数组获取当前周

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

我有一个带有日期键的对象数组,我想过滤掉具有当前周的对象,我如何实现 returns 我在 objects.i 的当前周尝试使用过滤,但我想首先我需要格式化日期键。

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

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

你可以:

  • 首先,通过 .week()
  • 获取一年中的第几周
  • 秒,按.year()
  • 获取年份
  • 最后筛选出与当前日期同年同周的日期

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>

您必须根据一月第一天创建具有 return 周数的函数,然后使用过滤器

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)

尝试使用

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);

希望对您有所帮助:)

也许你可以考虑使用 moment.js 库 https://github.com/moment/moment

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

然后通过此代码您将只收到本周的通知