Get Weeks In Month Through Javascript

后端 未结 17 1332
盖世英雄少女心
盖世英雄少女心 2020-11-30 09:03

In Javascript, how do I get the number of weeks in a month? I can\'t seem to find code for this anywhere.

I need this to be able to know how many rows I need for a g

17条回答
  •  执笔经年
    2020-11-30 09:43

        function weekCount(year, month_number, day_start) {
    
            // month_number is in the range 1..12
            // day_start is in the range 0..6 (where Sun=0, Mon=1, ... Sat=6)
    
            var firstOfMonth = new Date(year, month_number-1, 1);
            var lastOfMonth = new Date(year, month_number, 0);
    
            var dayOffset = (firstOfMonth.getDay() - day_start + 7) % 7;
            var used = dayOffset + lastOfMonth.getDate();
    
            return Math.ceil( used / 7);
        }
    

提交回复
热议问题