Last Updated: February 25, 2016
·
846
· 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
}

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside