Get Weeks In Month Through Javascript

后端 未结 17 1364
盖世英雄少女心
盖世英雄少女心 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:38

    using moment js

    function getWeeksInMonth(year, month){
    
            var monthStart     = moment().year(year).month(month).date(1);
            var monthEnd       = moment().year(year).month(month).endOf('month');
            var numDaysInMonth = moment().year(year).month(month).endOf('month').date();
    
            //calculate weeks in given month
            var weeks      = Math.ceil((numDaysInMonth + monthStart.day()) / 7);
            var weekRange  = [];
            var weekStart = moment().year(year).month(month).date(1);
            var i=0;
    
            while(i

提交回复
热议问题