function
printMonth(year, month) {
console.log(`\n${getMonthName(month)} ${year}`);
console.log(
"Mo Tu We Th Fr Sa Su"
);
const weekday = calculateWeekday(year, month);
const daysInMonth = getDaysInMonth(year, month);
for
(let i = 1; i <= daysInMonth + weekday - 1; i++) {
if
(i < weekday)
process.stdout.write(
" "
);
else
process.stdout.write(`${(i - weekday + 1).toString().padStart(2,
' '
)} ${((i + 1) % 7 === 0) ?
'\n'
:
' '
}`);
}
}
function
printCalendar(year) {
for
(let month = 1; month <= 12; month++) {
printMonth(year, month);
}
}
const currentYear = 2024;
printCalendar(currentYear);
function
getMonthName(month) {
const monthNames = [
""
,
"January"
,
"February"
,
"March"
,
"April"
,
"May"
,
"June"
,
"July"
,
"August"
,
"September"
,
"October"
,
"November"
,
"December"
];
return
monthNames[month];
}
function
calculateWeekday(year, month) {
return
(year * 365 + (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400 + month * 306 + 5) % 7;
}
function
getDaysInMonth(year, month) {
switch
(month) {
case
4:
case
6:
case
9:
case
11:
return
30;
case
2:
return
((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) ? 29 : 28;
default
:
return
31;
}
}