Created
January 27, 2020 18:58
-
-
Save brunolkatz/4e71e01822131f7a8d358ea484e8b245 to your computer and use it in GitHub Desktop.
Return a array of weeks in a month, with start and end date or day.
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
/** | |
* Return a array of weeks in a month, with start and end date or day. | |
* @param _month number Define the month (WARNING: month start at 0 in javascript) | |
* @param _year number Define the year | |
* @param returnDate boolean Define if returns a Date class in returned array or not | |
* @return Array<{start: number, end: number}> | |
*/ | |
public getWeeksStartAndEndInMonth(_month: number, _year: number, returnDate: boolean = false) { | |
const lastDay = new Date(_year, _month + 1, 0); | |
let weeks: Array<{start: number, end: number}> = []; | |
let _days = []; | |
for (let _day = 1; _day <= lastDay.getDate(); _day++){ | |
let _thisDay = new Date(_year, _month, _day); | |
_days.push(returnDate ? _thisDay : _day); | |
if(_thisDay.getDay() === 6){ | |
weeks.push({start: _days[0], end: _days[_days.length-1]}); | |
_days = []; | |
}else if(_thisDay.getDate() === lastDay.getDate()){ | |
weeks.push({start: _days[0], end: _days[_days.length-1]}); | |
} | |
} | |
return weeks; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment