Skip to content

Instantly share code, notes, and snippets.

@danielkpodo
Last active May 2, 2021 17:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielkpodo/4d81888fa43bfb36748b1653a3d1e01f to your computer and use it in GitHub Desktop.
Save danielkpodo/4d81888fa43bfb36748b1653a3d1e01f to your computer and use it in GitHub Desktop.
A function to generate week numbers given start date and number of months
import moment from "moment";
const startDate = "2020-09-23";
const months = 6;
const getWeeks = (weekRanges) => {
const weekNumbers = [];
weekRanges.forEach((date) => {
weekNumbers.push(moment(date.begin, date.end).week());
});
return weekNumbers;
};
const extractWeekRanges = (startDate, endDate) => {
let dates = [];
const addDays = function (days) {
const date = new Date(this.valueOf());
date.setDate(date.getDate() + days);
return date;
};
//now our Sunday check
let currentDate = formatDate(startDate);
if (currentDate.getDay() > 0) {
// console.log("not a sunday...adjusting");
currentDate.setDate(currentDate.getDate() - currentDate.getDay());
}
while (currentDate <= formatDate(endDate)) {
let endWeekDate = addDays.call(currentDate, 6);
dates.push({ begin: currentDate, end: endWeekDate });
currentDate = addDays.call(currentDate, 7);
}
return dates;
};
const formatDate = (dte) => {
return new Date(`${dte}`);
};
const addMonths = (startDate, n) => {
const date = new Date(`${startDate}`);
date.setMonth(date.getMonth() + n);
return date;
};
const weeks = (startDate, months) => {
const endDate = addMonths(startDate, months);
const weekRanges = extractWeekRanges(startDate, endDate);
return getWeeks(weekRanges);
};
console.log(weeks(startDate, months));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment