The year 2024 is a leap year, or bisiesto in Spanish. According to the Gregorian calendar, a year is considered a leap year if it is divisible by 4, except for end-of-century years which must be divisible by 400. This means that the year 2024, which is divisible by 4, has 366 days including the extra day, February 29.
This system keeps our calendar year synchronized with the solar year.
class Calendar {
constructor(year, month) {
this.year = year;
this.month = month;
}
getDaysInMonth() {
return new Date(this.year, this.month, 0).getDate();
}
getFirstDay() {
return new Date(this.year, this.month - 1, 1).getDay();
}
printCalendar() {
let firstDay = this.getFirstDay();
let daysInMonth = this.getDaysInMonth();
let str = 'Sun Mon Tue Wed Thu Fri Sat\n';
for (let i = 0; i < firstDay; i++) {
str += ' ';
}
for (let i = 1; i <= daysInMonth; i++) {
if (i < 10) {
str += ' ' + i + ' ';
} else {
str += i + ' ';
}
if ((i + firstDay) % 7 === 0) {
str += '\n';
}
}
return str;
}
}
let calendar = new Calendar(2024, 2); // February 2024
console.log(calendar.printCalendar());
py -m calendar 2024 2
py .\my_calendar.py