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

后端 未结 12 795
挽巷
挽巷 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:36

    I think it is a bit late to respond but I don't see the same solution as mine so I am posting.

    moment().weeks() - moment().add(0, 'month').startOf('month').weeks() + 1
    

    "moment().weeks()" returns the number of weeks for today within that year.

    "moment().add(0, 'month').startOf('month').weeks()" returns the number of weeks for the first day of the month within that year.

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

    Here's Robin Malfait's solution implemented with the lightweight library date-fns

    import {
        differenceInDays,
        startOfMonth,
        startOfWeek,
        getDate
    } from 'date-fns'
    
    const weekOfMonth = function (date) {
        const firstDayOfMonth = startOfMonth(date)
        const firstDayOfWeek = startOfWeek(firstDayOfMonth)
        const offset = differenceInDays(firstDayOfMonth, firstDayOfWeek)
        return Math.ceil((getDate(date) + offset) / 7)
    }
    
    export default weekOfMonth
    
    0 讨论(0)
  • 2020-12-03 14:38

    It seems that moment.js does not have the method that implements the functionality that you are looking for. However, you can find the nth number of a certain day of the week in a month is using the Math.ceil of the date / 7 For example:

    var firstFeb2014 = moment("2014-02-01"); //saturday
    var day = firstFeb2014.day(); //6 = saturday
    var nthOfMoth = Math.ceil(firstFeb2014.date() / 7); //1
    
    var eightFeb2014 = moment("2014-02-08"); //saturday, the next one
    console.log( Math.ceil(eightFeb2014.date() / 7) ); //prints 2, as expected
    

    It looks like this is the number you are looking for, as demonstrated by the following test

    function test(mJsDate){
       var str = mJsDate.toLocaleString().substring(0, 3) +
                 " number " + Math.ceil(mJsDate.date() / 7) +
                 " of the month";
       return str;
    }
    
    for(var i = 1; i <= 31; i++) {
       var dayStr = "2014-01-"+ i;
       console.log(dayStr + " " + test(moment(dayStr)) );
    }
    
    //examples from the console:
    //2014-01-8 Wed number 2 of the month
    //2014-01-13 Mon number 2 of the month
    //2014-01-20 Mon number 3 of the month
    //2014-01-27 Mon number 4 of the month
    //2014-01-29 Wed number 5 of the month
    
    0 讨论(0)
  • 2020-12-03 14:39

    Simple using moment.js

    function week_of_month(date) {
    
            prefixes = [1,2,3,4,5];
    
        return prefixes[0 | moment(date).date() / 7] 
    
    }
    
    0 讨论(0)
  • 2020-12-03 14:42

    I think the answer to this question will be helpful, even though it doesn't use moment.js as requested:

    Get week of the month

    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
提交回复
热议问题