Python Calendar Module – Complete Guide with Examples
The calendar module in Python is used to work with dates, months, and year-based calendars. It can display the calendar for a month or entire year, check leap years, count leap days, and more.
Python provides this module as part of its powerful standard library, which contains hundreds of ready-to-use functions for date calculations.
Why Use the Calendar Module?
- To print calendar of a month or a whole year
- To check whether a year is leap year
- To count number of leap years between two years
- To get weekday index for any date
- To design schedule/calendar-based applications
Basic Example of Calendar Module
import calendar
# Print calendar of March 2014
print(calendar.month(2014, 3))
# Get first weekday (0=Monday, 6=Sunday)
print(calendar.firstweekday())
# Check leap year
if calendar.isleap(2020):
print("The year is leap year")
else:
print("The year is not leap year")
# Count total leap years between two years
print(calendar.leapdays(1950, 2000))
Print Full Year Calendar
print("The calendar of year 2018 is:")
print(calendar.calendar(2018, 2, 1, 6))
Parameters:
- 2018 → Year
- 2 → Width between columns
- 1 → Number of lines per week
- 6 → Number of columns
Useful Calendar Module Functions
1. calendar.month(year, month)
Displays formatted month calendar.
2. calendar.calendar(year)
Displays full year calendar.
3. calendar.isleap(year)
Returns True if leap year.
4. calendar.weekday(year, month, day)
Returns weekday index (0 = Monday).
5. calendar.monthrange(year, month)
Returns (first_weekday, total_days_in_month).
Assignment 1: Calculate Age in Years, Months, Days
Solution by Shiva Sir:
from datetime import date
a = date(date.today().year, date.today().month, date.today().day)
b = date(
int(input("enter year")),
int(input("enter month")),
int(input("enter day"))
)
# Calculate month difference
if a.month > b.month:
months = a.month - b.month
else:
months = b.month - a.month
# Year difference
years = a.year - b.year
# Day difference
if a.day > b.day:
days = a.day - b.day
else:
days = b.day - a.day
print('{0} years, {1} months, {2} days'.format(years, months, days))
Assignment 2: Day Before & After Calculator
Write a program where user enters number of days and the script prints:
- Date before those days
- Date after those days
Hint: Use timedelta from datetime.
Assignment 3: Create a Stopwatch
Write a Python program where:
- Press s → Start
- Press p → Pause
- Press e → End
Hint: Use time.time() to capture start and end timestamps.
Extra Concepts Added for Better Understanding
1. Get Weekday Name
import calendar
days = ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]
print(days[ calendar.weekday(2025, 12, 25) ])
2. Get Total Days in a Month
import calendar
first, total = calendar.monthrange(2025, 5)
print("Total days:", total)
3. Generate Calendar as a Matrix
print(calendar.monthcalendar(2025, 1))
4. Iterate Through All Dates of a Month
import calendar
for week in calendar.monthcalendar(2025, 1):
print(week)
12 Comments
ABHISHEK GOYAL
ReplyDeleteWAP to calculate total age in year, month, and a day where the date of birth will be entered by the user?
from datetime import date
print("##### Welcome to age calculator ######")
birth_year = int(input("Enter your year of birth: \n"))
birth_month = int(input("Enter your month of birth: \n"))
birth_day = int(input("Enter your day of birth: \n"))
current_year = date.today().year
current_month = date.today().month
current_day = date.today().day
age_year = current_year - birth_year
age_month = abs(current_month-birth_month)
age_day = abs(current_day-birth_day)
print("Your exact age is: ", age_year, "Years", age_month, "months and", age_day, "days")
use this program to correct ouput
Deletefrom datetime import date
a = date(2021, 1, 5)
b = date(1996, 12, 13)
#print(type(a))
months = a.month - b.month
years = a.year - b.year
days = a.day - b.day
print('{0} years, {1} months, {2} days'.format(years, months, days))
import calendar
ReplyDeletefrom datetime import date
year = int(input("Enter your birth year = "))
month = int(input("Enter your birth month in numeric form = "))
day = int(input("Enter your birth date = "))
cy = (date.today().year)
cm = (date.today().month)
cd = (date.today().day)
y = cy-year
m = cm-month
d = cd-day
if d<0 or m<0:
y =y-1
m =m+12
d = d+30
print("You are ",y,"years ",m,"months ",d,"days old")
WAP to calculate total age in year, month, and a day where the date of birth will be entered by the user?
ReplyDeleteimport calendar
from datetime import date
year=int(input("Enter the your birth year ="))
month=int(input("Enter the your birth month ="))
day=int(input("Enter the your birth day ="))
current_yr=(date.today().year)
current_mt= (date.today().month)
current_dy=(date.today().day)
age_y=current_yr-year
age_m=current_mt-month
age_d=day-current_dy
if age_d<0 or age_m<0:
age_y=age_y-1
print("yor are ",age_y,"years",age_m,"months",age_d,"days olds")
#WAP to display before the day, after day when the user enters a number of days.
ReplyDeleteimport calendar
a=int(input("Enter the number of week ="))
days= {1:'monday',2:'tuesday',3:'wednesday',4:'thursday',5:'friday',6:'saturday',7:'sunday'}
for i ,j in days.items():
if a==i:
print("before day =",days[a-1])
print("today = ",days[i])
print("after day = ",days[a+1])
#WAP to create STOP watch when pressings s then start, p then pause and e then end?
import time
start=raw_input("press enter to start stopwatch")
print("timer has start")
begin=time.time()
endtimer =input("press 0 to exit the stopwatch ")
print("stopwatch end")
end=time.time()
elapsed=end-begin
elapsed=int(elapsed)
print(elapsed ,"seconds")
exit(0)
import datetime
ReplyDeletecy = datetime.date.today().year
cm = datetime.date.today().month
cd = datetime.date.today().day
by=int(input("enter your birth year"))
bm=int(input("enter your birth month"))
bd=int(input("enter your birth date"))
ay=cy-by
am=abs(cm-bm)
ad=abs(cd-bd)
print(ay,am,ad)
WAP to calculate total age in year, month, and a day where the date of birth will be entered by the user?
ReplyDeleteimport datetime
current_year = datetime.date.today().year
current_month = datetime.date.today().month
current_day = datetime.date.today().day
birth_year = int(input("enter your birth year "))
birth_month = int(input("enter your birth month "))
birth_date = int(input("enter your birth date "))
y=current_year-birth_year
m=current_month-birth_month
d=current_day-birth_date
print("your age is :" , y,"years",m,"months",d,"days")
n=int(input("enter a number of day"))
ReplyDeleteif (n<=7):
if (n==1):
print("TODAY IS SUNDAY NEXT DAY MONDAY BEFORE DAY SATURDAY")
elif (n==2):
print ("TODAY IS MONDAY NEXT DAY TUESDAY BEFORE DAY SUNDAY")
elif (n==3):
print ("TODAY IS TUESDAY NEXT DAY WEDNESDAY BEFORE DAY MONDAY")
elif (n==4):
print ("TODAY IS WEDNESDAY NEXT DAY THURSDAY BEFORE DAY TUESDAY")
elif (n==5):
print ("TODAY IS THURSDAY NEXT DAY FRIDAY BEFORE DAY WEDNESDAY")
elif (n==6):
print ("TODAY IS FRIDAY NEXT DAY SATURDAY BEFORE DAY THURSDAY")
else:
print("TODAY IS SATURDAY NEXT DAY SUNDAY BEFORE DAY FRIDAY")
else:
printf ("Invalid Entry")
from datetime import date, timedelta
ReplyDeletes=int(input("enter days"))
current_date = date.today().isoformat()
days_before = (date.today()-timedelta(days=s)).isoformat()
days_after = (date.today()+timedelta(days=s)).isoformat()
print("\nCurrent Date: ",current_date)
print(s,"days before current date: ",days_before)
print(s,"days after current date : ",days_after)
from datetime import date
ReplyDeleteprint("AGE calculator")
user_date=int(input("please fill day of birth : "))
user_month=int(input("please fill month of birth : "))
user_year=int(input("please fill year of birth : "))
day = date.today().day
month = date.today().month
year = date.today().year
ageday= day-user_date
agemonth= month-user_month
ageyear=year-user_year
print("your current age is :",ageday,'/',agemonth,'/',ageyear)
from datetime import date
ReplyDeleteprint("AGE calculator")
user_date=int(input("please fill day of birth : "))
user_month=int(input("please fill month of birth : "))
user_year=int(input("please fill year of birth : "))
day = date.today().day
month = date.today().month
year = date.today().year
ageday= day-user_date
agemonth= month-user_month
ageyear=year-user_year
print("your current age is :",ageday,'/',agemonth,'/',ageyear)
from datetime import date
ReplyDeleteday,month,year=int(input("Enter day:-")),int(input("Enter Month:-")),int(input("Enter year:-"))
y,m,d=date.today().year,date.today().month,date.today().day
if m>month:
if d>day:
a=y-year
b=m-month
c=d-day
else:
a=y-year
b=m-month
c=30-(day-d)
else:
if day>d:
a=(y-year)-1
b=12-(month-m)-1
c=30-(day-d)
else:
a=(y-year)-1
b=12-(month-m)
c=d-day
print("Age is {} years {} months and {} days".format(a,b,c))
POST Answer of Questions and ASK to Doubt