Get Weeks In Month Through Javascript

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

    This piece of code give you the exact number of weeks in a given month:

    Date.prototype.getMonthWeek = function(monthAdjustement)
    {       
        var firstDay = new Date(this.getFullYear(), this.getMonth(), 1).getDay();
        var returnMessage = (Math.ceil(this.getDate()/7) + Math.floor(((7-firstDay)/7)));
        return returnMessage;
    }
    

    The monthAdjustement variable adds or substract the month that you are currently in

    I use it in a calendar project in JS and the equivalent in Objective-C and it works well

提交回复
热议问题