There is a simple way-
You can use the calendar
module in Python to create a calendar for the current year. But it defeats the purpose – of getting your hands dirty by writing big lengthy Python Code. But anyway i am adding it here:-
You can use the calendar
module in Python to create a calendar for the current year. Here’s a simple Python program to achieve that.
import calendar
def create_calendar(year):
# Create a TextCalendar instance
cal = calendar.TextCalendar(calendar.SUNDAY) # Start week on Sunday
# Generate the calendar for the given year
calendar_text = cal.formatyear(year, 2, 1, 1, 3)
# 2 spaces between months, 1 row between months, 1 column between months, 3 months per row
return calendar_text
def main():
# Get the current year
current_year = calendar.datetime.datetime.now().year
# Create calendar for the current year
calendar_text = create_calendar(current_year)
# Display the calendar
print(f"Calendar for the year {current_year}:n")
print(calendar_text)
if __name__ == "__main__":
main()
This program creates a calendar for the current year using the TextCalendar
class from the calendar
module. It then formats the calendar as text and prints it to the console. The calendar starts the week on Sunday and formats the months with 2 spaces between them, 1 row between them, 1 column between them, and displays 3 months per row. You can adjust these parameters based on your preferences.
Creating a calendar without importing the calendar
package involves implementing the logic for generating the calendar manually. Here’s a Python program that generates a calendar for the current year without using the calendar
package:
import datetime
def is_leap_year(year):
"""Check if a year is a leap year."""
if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0):
return True
return False
def get_month_days(year, month):
"""Get the number of days in a month."""
if month in {1, 3, 5, 7, 8, 10, 12}:
return 31
elif month in {4, 6, 9, 11}:
return 30
elif is_leap_year(year):
return 29
else:
return 28
def create_calendar(year):
"""Create a calendar for the given year."""
months = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"]
calendar_text = ""
for month_num, month_name in enumerate(months, start=1):
calendar_text += f"n{month_name}n"
calendar_text += "Mo Tu We Th Fr Sa Sun"
# Get the first day of the month
first_day = datetime.date(year, month_num, 1).weekday()
# Fill the leading spaces
calendar_text += " " * first_day
# Fill in the days of the month
for day in range(1, get_month_days(year, month_num) + 1):
calendar_text += f"{day:2d} "
first_day += 1
if first_day == 7:
calendar_text += "n"
first_day = 0
calendar_text += "n"
return calendar_text
def main():
# Get the current year
current_year = datetime.datetime.now().year
# Create calendar for the current year
calendar_text = create_calendar(current_year)
# Display the calendar
print(f"Calendar for the year {current_year}:n")
print(calendar_text)
if __name__ == "__main__":
main()
This program calculates the days of the week and fills in the dates for each month manually. It also accounts for leap years to determine the number of days in February. The generated calendar is printed to the console for the current year.
Leave a Reply