Created
November 26, 2019 17:36
-
-
Save elib0/db453903b7d51770427f2fc777eadf88 to your computer and use it in GitHub Desktop.
Retorna rango de fecha(inicio - fin) de un mes
This file contains hidden or 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
getWeeksInMonth(year: number, month: number) { | |
const weeks = []; | |
const firstDay: Date = new Date(year, month, 1); | |
const lastDay: Date = new Date(year, month + 1, 0); | |
const daysInMonth: number = lastDay.getDate(); | |
let dayOfWeek: number = firstDay.getDay(); | |
let start: number; | |
let end: number; | |
for (let i = 1; i < daysInMonth + 1; i++) { | |
if (dayOfWeek === 0 || i === 1) { | |
start = i; | |
} | |
if (dayOfWeek === 6 || i === daysInMonth) { | |
end = i; | |
if (start !== end) { | |
weeks.push({ | |
start: start, | |
end: end | |
}); | |
} | |
} | |
dayOfWeek = new Date(year, month, i).getDay(); | |
} | |
return weeks; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment