class DayModel {
constructor(year, month, date) {
this.year = year
this.month = month
this.date = date
}
isEqual(day) {
if (day) {
return (
this.year === day.year &&
this.month === day.month &&
this.date === day.date
)
}
return false
}
toDate() {
return new Date(this.year, this.month, this.date)
}
incrementBy(value) {
const date = new Date(this.year, this.month, this.date + value)
return new DayModel(date.getFullYear(), date.getMonth(), date.getDate())
}
clone() {
return new DayModel(this.year, this.month, this.date)
}
toComparisonString() {
return `${this.year}${this.pad(this.month)}${this.pad(this.date)}`
}
toDateString() {
return this.toDate().toLocaleDateString(undefined, {
weekday: "long",
month: "long",
day: "numeric",
year: "numeric"
})
}
isBefore(day, dayInclusive = false) {
return dayInclusive ?
this.toDate().getTime() <= day?.toDate().getTime() :
this.toDate().getTime() < day?.toDate().getTime()
}
isAfter(day, dayInclusive = false) {
return dayInclusive ?
this.toDate().getTime() >= day?.toDate().getTime() :
this.toDate().getTime() > day?.toDate().getTime()
}
pad(num) {
return num < 10 ? `0${num}` : `${num}`;
}
}
function getDaysInMonthJS(month, year) {
const date = new Date(year, month, 1);
const days = [];
while (date.getMonth() === month) {
days.push(new Date(date));
date.setDate(date.getDate() + 1);
}
return days;
}
function getDaysInMonthTS(month, year) {
const nbD = new Date(year, month + 1, 0).getDate();
return Array(nbD)
.fill(null)
.map((_date, index) => {
return new DayModel(year, month, index + 1);
});
}