Get Weeks In Month Through Javascript

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

    This is very simple two line code. and i have tested 100%.

    Date.prototype.getWeekOfMonth = function () {
        var firstDay = new Date(this.setDate(1)).getDay();
        var totalDays = new Date(this.getFullYear(), this.getMonth() + 1, 0).getDate();
        return Math.ceil((firstDay + totalDays) / 7);
    }
    

    How to use

    var totalWeeks = new Date().getWeekOfMonth();
    console.log('Total Weeks in the Month are : + totalWeeks ); 
    

提交回复
热议问题