-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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