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

后端 未结 12 835
挽巷
挽巷 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条回答
  •  猫巷女王i
    2020-12-03 14:42

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

提交回复
热议问题