Skip to content

Instantly share code, notes, and snippets.

@Avi-E-Koenig
Created June 18, 2020 10:04
Show Gist options
  • Save Avi-E-Koenig/2bbb02ef96fcfb171e7b5e8fd261c6f9 to your computer and use it in GitHub Desktop.
Save Avi-E-Koenig/2bbb02ef96fcfb171e7b5e8fd261c6f9 to your computer and use it in GitHub Desktop.
get arr of dates in isoString for the period between months in a year.
class DateArrMaker {
constructor(minMonth, maxMonth, year) {
if (isNaN(minMonth) || isNaN(maxMonth) || isNaN(year)) {
throw new Error(`all values must be numeric`);
}
// if (maxMonth < minMonth) {
// let temp = maxMonth;
// minMonth = maxMonth;
// maxMonth = temp;
// }
if (maxMonth < minMonth) {
throw new Error(
`maxMonth:"${maxMonth}" must be equal to or greater than minMonth:"${minMonth}"`,
);
}
this.minMonth = minMonth;
this.maxMonth = maxMonth + 1;
this.year = year;
this.dateArr = [];
return this.getArr();
}
getDaysInMonth(month) {
return new Date(this.year, month, 0).getDate();
}
buildArr() {
let period = this.maxMonth - this.minMonth;
let currentIterableMonthNum = this.minMonth;
for (let i = 0; i < period; i++) {
let daysInMonth = this.getDaysInMonth(currentIterableMonthNum);
console.log('DateArrMaker -> buildArr -> daysInMonth', daysInMonth);
for (let j = 0; j < daysInMonth; j++) {
let dayInMonth = j + 1;
console.log(
'DateArrMaker -> buildArr -> dayInMonth',
`${this.year}-${currentIterableMonthNum}-${dayInMonth}`,
);
let date = new Date(
`${this.year}-${currentIterableMonthNum}-${dayInMonth}`,
);
this.dateArr.push(date);
}
currentIterableMonthNum++;
}
}
getArr() {
this.buildArr();
return this.dateArr;
}
}
module.exports = { DateArrMaker };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment