Skip to content

Instantly share code, notes, and snippets.

@hamswaldenkv
Forked from markthiessen/getWeeksInMonth.js
Created August 29, 2020 09:22
Show Gist options
  • Save hamswaldenkv/6c802caf2cb34b49f690f0c657e95e6f to your computer and use it in GitHub Desktop.
Save hamswaldenkv/6c802caf2cb34b49f690f0c657e95e6f to your computer and use it in GitHub Desktop.
JavaScript - get weeks in a month as array of start and end days
//note: month is 0 based, just like Dates in js
function getWeeksInMonth(month, year){
var weeks=[],
firstDate=new Date(year, month, 1),
lastDate=new Date(year, month+1, 0),
numDays= lastDate.getDate();
var start=1;
var end=7-firstDate.getDay();
while(start<=numDays){
weeks.push({start:start,end:end});
start = end + 1;
end = end + 7;
if(end>numDays)
end=numDays;
}
return weeks;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment