Skip to content

Instantly share code, notes, and snippets.

@jrran90
Forked from markthiessen/getWeeksInMonth.js
Created March 22, 2019 09:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jrran90/45d5ebd1d6240bd9c24ba0d71cb01506 to your computer and use it in GitHub Desktop.
Save jrran90/45d5ebd1d6240bd9c24ba0d71cb01506 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