Skip to content

Instantly share code, notes, and snippets.

@elib0
Created November 26, 2019 17:36
Show Gist options
  • Save elib0/db453903b7d51770427f2fc777eadf88 to your computer and use it in GitHub Desktop.
Save elib0/db453903b7d51770427f2fc777eadf88 to your computer and use it in GitHub Desktop.
Retorna rango de fecha(inicio - fin) de un mes
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