Get Weeks In Month Through Javascript

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

    You could use my time.js library. Here's the weeksInMonth function:

    // http://github.com/augustl/time.js/blob/623e44e7a64fdaa3c908debdefaac1618a1ccde4/time.js#L67
    
    weeksInMonth: function(){
      var millisecondsInThisMonth = this.clone().endOfMonth().epoch() - this.clone().firstDayInCalendarMonth().epoch();
      return Math.ceil(millisecondsInThisMonth / MILLISECONDS_IN_WEEK);
    },
    

    It might be a bit obscure since the meat of the functionality is in endOfMonth and firstDayInCalendarMonth, but you should at least be able to get some idea of how it works.

提交回复
热议问题