-
-
Save NerdGr8/c322fe1c44b19ed3fa259012a4652dde 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; | |
} |
Author
NerdGr8
commented
Jul 11, 2018
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment