Moment.js how to get week of month? (google calendar style)

后端 未结 12 796
挽巷
挽巷 2020-12-03 13:56

I am using Moment.js and it is great. The problem I have now is that I can\'t figure out how to get the week of the month a certain date is. I can only find \"week of year\"

相关标签:
12条回答
  • 2020-12-03 14:47

    When calculating the week of the month based on a given date, you have to take the offset into account. Not all months start on the first day of the week.

    If you want to take this offset into account, you can use something something like the following if you are using moment.

    function weekOfMonth (input = moment()) {
      const firstDayOfMonth = input.clone().startOf('month');
      const firstDayOfWeek = firstDayOfMonth.clone().startOf('week');
    
      const offset = firstDayOfMonth.diff(firstDayOfWeek, 'days');
    
      return Math.ceil((input.date() + offset) / 7);
    }
    
    0 讨论(0)
  • 2020-12-03 14:49

    There is a problem with @Daniel Earwicker answer. I was using his function in my application and the while loop was infinite because of the following situation:

    I was trying to figure out which week of december (2016) was the day 31. the first day of december was day 336 of the year. The last day of december was day 366 of the year.

    Problem here: When it was day 366 (31 of december, last day of the year) the code added another day to this date. But with another day added it would be day 1 of january of 2017. Therefore the loop never ended.

     while (m.dayOfYear() <= yearDay) { 
    
        if (m.day() == weekDay) {
            count++; 
        }
        m.add('days', 1); 
    }
    

    I added the following lines to the code so the problem would be fixed:

    function countWeekdayOccurrencesInMonth(date) {
    
      var m = moment(date),
            weekDay = m.day(),
            yearDay = m.dayOfYear(),
            year = m.year(),
            count = 0;
    
     m.startOf('month');
    
     while (m.dayOfYear() <= yearDay && m.year() == year) {
        if (m.day() == weekDay) {
            count++;
        }
        m.add('days', 1);
     }
    
     return count;
    }
    

    It verifies if it is still in the same year of the date being veryfied

    0 讨论(0)
  • 2020-12-03 14:50

    This library adds the function moment.weekMonth() https://github.com/c-trimm/moment-recur

    0 讨论(0)
  • 2020-12-03 14:53

    What about something like:

    weekOfCurrentMonth = (moment().week() - (moment().month()*4));
    

    This takes the current week of the year, and subtracts it by the 4 times the number of previous months. Which should give you the week of the current month

    0 讨论(0)
  • 2020-12-03 14:55

    I'd do the following:

    let todaysDate = moment(moment.now());
    let endOfLastMonth = moment(get(this, 'todaysDate')).startOf('month').subtract(1, 'week');
    
    let weekOfMonth = todaysDate.diff(endOfLastMonth, 'weeks');
    

    That gets todaysDate and the endOfLastMonth and then uses Moment's built-in diff() method to compute the current month's week number.

    0 讨论(0)
  • 2020-12-03 14:55

    Using moment.js,

    let count = 0;
     for (let i = 1; i <= moment().daysInMonth(); i += 7) {
          if (i < today.format('D')) {
             count += 1;
          }
         else {
           break;
         }
     };
    

    count gives the occurrence of the day with respect to week in the month

    0 讨论(0)
提交回复
热议问题