Get Weeks In Month Through Javascript

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

    None of the solutions here really worked for me. Here is my crack at it.

    // Example
    // weeksOfMonth(2019, 9) // October
    // Result: 5
    weeksOfMonth (year, monthIndex) {
      const d = new Date(year, monthIndex+ 1, 0)
      const adjustedDate = d.getDate() + d.getDay()
      return Math.ceil(adjustedDate / 7)
    }
    

提交回复
热议问题