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
}Written by Matthias Le Brun
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Date
Authors
goker
24.62K
raviolicode
16.36K
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
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#