Python Date & Time Tutorial | datetime, time & calendar Module Explained
Almost every software application works with dates and time. Whether you’re building an attendance system, booking application, e-commerce platform, AI logging system, banking software, or social media application, managing date and time correctly is essential.
Python provides powerful built-in modules such as datetime, time, and calendar that help developers work with dates, timestamps, durations, scheduling, and time calculations efficiently.
In this lesson, you’ll learn how to create dates and times, format them, calculate differences between dates, work with timestamps, and build practical applications using Python’s date and time modules.
By the end of this lesson, you’ll confidently manage date and time operations in professional Python applications.
Learning Objectives
After completing this lesson, you will be able to:
- Understand Python’s date and time modules.
- Work with the
datetimemodule. - Format dates and times.
- Calculate date differences.
- Use timestamps.
- Work with calendars.
- Build scheduling applications.
- Apply date and time concepts in real-world projects.
Topics Covered
- Introduction to Date & Time
- datetime Module
- date Object
- time Object
- Current Date & Time
- Formatting Dates
- Date Arithmetic
- Timestamps
- calendar Module
- Real-World Applications
- Best Practices
Detailed Lesson Content
Introduction to Date & Time
Imagine you’re building an Online Course Management System.
The application needs to manage:
- Student registration date
- Course start date
- Assignment deadlines
- Exam schedules
- Attendance timestamps
- Certificate issue dates
All these features rely on accurate date and time management.
Python provides built-in modules to make these operations simple and reliable.
Importing the datetime Module
from datetime import datetime
The datetime module is the most commonly used module for working with dates and times.
Getting Current Date and Time
Example
from datetime import datetime
now = datetime.now()
print(now)
Output
2026-07-10 15:45:21.678921
The output includes:
- Year
- Month
- Day
- Hour
- Minute
- Second
- Microsecond
Getting Current Date
from datetime import date
today = date.today()
print(today)
Output
2026-07-10
Getting Current Time
from datetime import datetime
current_time = datetime.now().time()
print(current_time)
Output
15:45:21.678921
Accessing Individual Components
from datetime import datetime
now = datetime.now()
print(now.year)
print(now.month)
print(now.day)
print(now.hour)
print(now.minute)
print(now.second)
Output
2026
7
10
15
45
21
Creating a Specific Date
from datetime import datetime
exam = datetime(2026,12,15,10,30)
print(exam)
Output
2026-12-15 10:30:00
Formatting Dates
Use the strftime() method.
Example
from datetime import datetime
today = datetime.now()
print(today.strftime("%d-%m-%Y"))
Output
10-07-2026
Common Format Codes
| Format | Meaning | Example |
|---|---|---|
%d |
Day | 10 |
%m |
Month | 07 |
%Y |
Full Year | 2026 |
%y |
Two-digit Year | 26 |
%H |
Hour (24-hour) | 15 |
%I |
Hour (12-hour) | 03 |
%M |
Minute | 45 |
%S |
Second | 21 |
%A |
Weekday | Friday |
%B |
Month Name | July |
Converting String to Date
Use strptime().
Example
from datetime import datetime
date_string = "15-12-2026"
exam = datetime.strptime(date_string,"%d-%m-%Y")
print(exam)
Output
2026-12-15 00:00:00
Date Arithmetic
from datetime import datetime
today = datetime.now()
future = datetime(2026,12,31)
difference = future - today
print(difference.days)
Output
174
Python automatically calculates the number of days.
Using timedelta
from datetime import datetime, timedelta
today = datetime.now()
next_week = today + timedelta(days=7)
print(next_week)
Output
2026-07-17
Working with Timestamps
A timestamp represents the number of seconds since January 1, 1970 (Unix Epoch).
from datetime import datetime
timestamp = datetime.now().timestamp()
print(timestamp)
Example Output
1783698325.55
Converting Timestamp to Date
from datetime import datetime
timestamp = 1783698325
date = datetime.fromtimestamp(timestamp)
print(date)
Working with calendar Module
Import calendar
import calendar
Display current month
import calendar
print(calendar.month(2026,7))
Output
July 2026
Mo Tu We Th Fr Sa Su
...
Finding Leap Year
import calendar
print(calendar.isleap(2028))
Output
True
Real-World Example 1: Student Attendance
from datetime import datetime
attendance_time = datetime.now()
print("Attendance Recorded At:")
print(attendance_time)
Real-World Example 2: Certificate Generation
from datetime import date
issue_date = date.today()
print("Certificate Issued On:")
print(issue_date)
Real-World Example 3: Event Countdown
from datetime import datetime
today = datetime.now()
event = datetime(2026,12,31)
remaining = event - today
print(remaining.days)
Real-World Example 4: Digital Clock
from datetime import datetime
while True:
print(
datetime.now().strftime("%H:%M:%S")
)
break



