Last Updated: February 25, 2016
·
642
· mlb

Get a range of the number of days contained by the following months

function getMonthRange(n){
  var i = 0
    , newDate = new Date()
    , year = newDate.getFullYear()
    , month = newDate.getMonth()
    , result = Array(n)
    , date
    , isLastMonth

  for(;i < n; i++) {
    isLastMonth = month == 12
    date = new Date(isLastMonth ? ++year : year, isLastMonth ? (month = 1) : ++month, 0)

    result[i] = {
      days : date.getDate(),
      month : month,
      year : year
    }
  }
  return result
}

This function returns an Array (with a length of n) that contains for each month :

{
   days : number,
   month : number,
   year : number
}