【问题标题】:How to get Current week from given array of objects date in javascript如何从javascript中的给定对象日期数组中获取当前周
【发布时间】:2021-02-10 06:27:30
【问题描述】:

我有一个带有日期键的对象数组,我想过滤掉具有当前周的对象,我怎样才能实现这个返回我当前周的对象。我尝试使用过滤器,但首先我需要格式化日期我猜是关键。

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

    }
  ]

【问题讨论】:

    标签: javascript reactjs momentjs


    【解决方案1】:

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

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

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

    【讨论】:

    • 如果我想从给定的日期键中提取时间怎么办,如何用时刻来完成。
    • 这有很大的减号 - moment.js 有很大的捆绑包,就像他们说的'在大多数情况下,你应该选择一个不同的库'。因此,如果只是使用 moment.js 的有趣项目,不要让你学到很多东西。如果将这么大的库用于这么小的任务是商业的......
    【解决方案2】:
     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);
    }
    

    【讨论】:

      【解决方案3】:

      你可以:

      • 首先,通过.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>

      【讨论】:

        【解决方案4】:

        您必须根据 1 月 1 日的天数创建返回周数的函数,然后使用过滤器

        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)

        【讨论】:

          【解决方案5】:

          尝试使用

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

          希望这会有所帮助:)

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2023-02-08
            • 1970-01-01
            • 2013-07-25
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-05-08
            相关资源
            最近更新 更多