Python provides the built-in datetime module for working with
dates, times, time differences, and date/time formatting.
The datetime module is commonly used in applications such as attendance systems,
booking systems, reports, billing software, scheduling applications, and many other projects.
datetime module provides classes such as date,
time, datetime, and timedelta.
The datetime module can be imported using:
import datetime
You can then access classes and functions using the module name.
import datetime
today = datetime.date.today()
print(today)
2026-09-20
The date.today() method returns the current local date according to the system clock.
from datetime import date
today = date.today()
print(today)
2026-09-20
The date object provides the year, month, and day attributes.
from datetime import date
today = date.today()
print("Year:", today.year)
print("Month:", today.month)
print("Day:", today.day)
Year: 2026 Month: 9 Day: 20
You can create a specific date using date(year, month, day).
from datetime import date
birthday = date(2000, 5, 15)
print(birthday)
2000-05-15
The datetime.now() method returns the current local date and time according to the system clock.
from datetime import datetime
now = datetime.now()
print(now)
2026-09-20 16:30:25.123456
The exact output depends on the date and time when the program runs.
The datetime.now() result contains both date and time.
You can access the time components using hour, minute, second, and microsecond.
from datetime import datetime
now = datetime.now()
print("Hour:", now.hour)
print("Minute:", now.minute)
print("Second:", now.second)
The time class can be used to represent a time without a date.
from datetime import time
t = time(14, 30, 45)
print(t)
14:30:45
from datetime import time
t = time(14, 30, 45)
print("Hour:", t.hour)
print("Minute:", t.minute)
print("Second:", t.second)
Hour: 14 Minute: 30 Second: 45
The datetime class can represent both date and time.
from datetime import datetime
dt = datetime(2026, 9, 20, 10, 30, 0)
print(dt)
2026-09-20 10:30:00
The strftime() method converts a date or datetime object into a formatted string.
from datetime import datetime
now = datetime.now()
formatted = now.strftime("%d-%m-%Y")
print(formatted)
20-09-2026
| Code | Meaning | Example |
|---|---|---|
%Y |
Four-digit year | 2026 |
%y |
Two-digit year | 26 |
%m |
Month as number | 09 |
%d |
Day of month | 20 |
%H |
Hour, 24-hour format | 16 |
%I |
Hour, 12-hour format | 04 |
%M |
Minute | 30 |
%S |
Second | 25 |
%p |
AM or PM | PM |
from datetime import datetime
dt = datetime(2026, 9, 20, 16, 30)
print(dt.strftime("%d/%m/%Y"))
print(dt.strftime("%I:%M %p"))
20/09/2026 04:30 PM
The strptime() method converts a string into a datetime object according to a specified format.
from datetime import datetime
date_string = "20-09-2026"
date_object = datetime.strptime(
date_string,
"%d-%m-%Y"
)
print(date_object)
2026-09-20 00:00:00
The timedelta class represents a duration or difference between dates and times.
from datetime import date, timedelta
today = date.today()
future_date = today + timedelta(days=10)
print(future_date)
If today is 2026-09-20, the result will be:
2026-09-30
from datetime import date, timedelta
today = date.today()
previous_date = today - timedelta(days=7)
print(previous_date)
Here, seven days are subtracted from the current date.
from datetime import datetime, timedelta
now = datetime.now()
future = now + timedelta(
hours=2,
minutes=30
)
print(future)
timedelta can represent days, seconds, microseconds, milliseconds, minutes, hours, and weeks.
from datetime import date
start = date(2026, 9, 1)
end = date(2026, 9, 20)
difference = end - start
print(difference.days)
19
Date objects can be compared using operators such as <, >, ==, and !=.
from datetime import date
date1 = date(2026, 9, 10)
date2 = date(2026, 9, 20)
if date1 < date2:
print("date1 is earlier")
date1 is earlier
The weekday() method returns a number representing the day of the week.
Monday is 0 and Sunday is 6.
from datetime import date
today = date.today()
print(today.weekday())
The isoweekday() method uses Monday as 1 and Sunday as 7.
The %A format code returns the full weekday name.
from datetime import date
today = date.today()
print(today.strftime("%A"))
Sunday
The exact result depends on the date when the program runs.
The %B format code returns the full month name.
from datetime import date
today = date.today()
print(today.strftime("%B"))
September
The isoformat() method returns a standard ISO-style representation.
from datetime import date
today = date.today()
print(today.isoformat())
2026-09-20
from datetime import datetime
now = datetime.now()
print("Date:", now.strftime("%d-%m-%Y"))
print("Time:", now.strftime("%I:%M:%S %p"))
print("Day:", now.strftime("%A"))
print("Month:", now.strftime("%B"))
Date: 20-09-2026 Time: 04:30:25 PM Day: Sunday Month: September
The combine() method combines a date object and a time object into a datetime object.
from datetime import date, time, datetime
d = date(2026, 9, 20)
t = time(10, 30)
dt = datetime.combine(d, t)
print(dt)
2026-09-20 10:30:00
Date and datetime objects are immutable, so methods such as replace() create a new object rather than modifying the original.
from datetime import date
old_date = date(2026, 9, 20)
new_date = old_date.replace(
year=2027
)
print(old_date)
print(new_date)
2026-09-20 2027-09-20
For applications that need reliable timezone handling, Python supports timezone-aware datetime objects.
The standard library provides timezone and zoneinfo.
from datetime import datetime, timezone
now = datetime.now(timezone.utc)
print(now)
This creates a timezone-aware datetime representing the current time in UTC.
The zoneinfo module provides access to the system's IANA time zone database when available.
from datetime import datetime
from zoneinfo import ZoneInfo
india_time = datetime.now(
ZoneInfo("Asia/Kolkata")
)
print(india_time)
This creates a timezone-aware datetime for the Asia/Kolkata time zone.
| Class | Purpose |
|---|---|
date |
Represents a calendar date. |
time |
Represents a time of day. |
datetime |
Represents both date and time. |
timedelta |
Represents a duration or difference between dates/times. |
timezone |
Represents a fixed UTC offset. |
from datetime import date
birth_date = date(2000, 5, 15)
today = date.today()
age = today.year - birth_date.year
if (today.month, today.day) < (
birth_date.month,
birth_date.day
):
age -= 1
print("Age:", age)
This example calculates age by comparing the birthday with today's date.
from datetime import date, timedelta
start_date = date.today()
due_date = start_date + timedelta(days=30)
print("Start Date:", start_date)
print("Due Date:", due_date)
This type of calculation can be useful for subscriptions, assignments, bills, bookings, and payment due dates.
datetime module for date and time operations.date represents a calendar date.time represents a time of day.datetime represents both date and time.timedelta represents a duration or difference.date.today() returns the current local date according to the system clock.datetime.now() returns the current local date and time.strftime() converts date/time objects into formatted strings.strptime() converts a string into a datetime object.weekday() returns the weekday number from 0 to 6.zoneinfo can be used for named time zones.Question: Which method is used to format a datetime object into a string?