Skip to content

Instantly share code, notes, and snippets.

@pereira-a
Created March 5, 2021 18:31
Show Gist options
  • Save pereira-a/e8c24daa2f9c318cec7824c3e48a00bf to your computer and use it in GitHub Desktop.
Save pereira-a/e8c24daa2f9c318cec7824c3e48a00bf to your computer and use it in GitHub Desktop.
get month's bussiness days (javascript)
function getWeeksInMonth(year: number, month: number) {
const weeks = [],
firstDate = new Date(year, month, 1),
lastDate = new Date(year, month + 1, 0),
numDays = lastDate.getDate();
let offset = 0;
if(firstDate.getDay() === 0) offset = 1;
else if(firstDate.getDay() === 6) offset = 2;
let start = 1 + offset;
let end: number;
if(offset === 0) end = 5 - firstDate.getDay() + 1;
else end = 5 - start + 1 + (offset * 2);
while(start <= numDays) {
weeks.push({start: start, end: end});
start = end + 3;
end = end + 7;
end = start === 1 && end === 8 ? 1 : end;
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