📅 Python Tutorial — Datetime Module
Introduction 🌟
The datetime module in Python provides classes for working with dates, times, and timestamps. It is widely used in applications like logs, billing, scheduling systems, automation, and time-based calculations.
Note
💡 Main classes: date, time, datetime, timedelta
💡 Supports formatting, parsing, arithmetic, timezone handling
💡 Supports formatting, parsing, arithmetic, timezone handling
1. Importing datetime 🧱
import_datetime.py
import datetime2. Working with date 📆
Create a Date Object
date_object.py
import datetime
d = datetime.date(2025, 1, 20)
print(d)Get Current Date
current_date.py
today = datetime.date.today()
print(today)Access Year, Month, Day
date_components.py
d = datetime.date.today()
print(d.year)
print(d.month)
print(d.day)3. Working with time ⏰
Create a Time Object
time_object.py
t = datetime.time(10, 30, 45)
print(t)Access Time Components
time_components.py
t = datetime.time(10, 30, 45, 200000)
print(t.hour, t.minute, t.second, t.microsecond)4. Working with datetime (Date + Time) 🕰️
Create a datetime object
datetime_object.py
dt = datetime.datetime(2025, 1, 20, 10, 30, 0)
print(dt)Current Date & Time
now.py
now = datetime.datetime.now()
print(now)Current UTC Time
utcnow.py
utc = datetime.datetime.utcnow()
print(utc)5. Formatting Dates & Times — strftime() 📝
strftime_example.py
now = datetime.datetime.now()
print(now.strftime("%Y-%m-%d"))
print(now.strftime("%d/%m/%Y"))
print(now.strftime("%I:%M %p"))
print(now.strftime("%A, %B %d, %Y"))| Format Code | Meaning |
|---|---|
| %Y | Year (2025) |
| %m | Month (01-12) |
| %d | Day (01-31) |
| %H | Hour (00-23) |
| %I | Hour (01-12) |
| %M | Minutes |
| %S | Seconds |
| %p | AM/PM |
| %A | Weekday name |
| %B | Month name |
6. Parsing Strings into Dates — strptime() 📥
strptime_example.py
dt = datetime.datetime.strptime("20-01-2025", "%d-%m-%Y")
print(dt)✔ Useful for reading dates from user input, APIs, files
7. Timedelta — Date Arithmetic ➕➖
timedelta_example.py
from datetime import datetime, timedelta
today = datetime.now()
tomorrow = today + timedelta(days=1)
past = today - timedelta(days=30)
print("Tomorrow:", tomorrow)
print("30 days ago:", past)Difference Between Two Dates
date_difference.py
d1 = datetime(2025, 1, 1)
d2 = datetime(2025, 2, 1)
delta = d2 - d1
print(delta.days)8. Working with Timezones — timezone ⏳
timezone_example.py
from datetime import datetime, timezone, timedelta
utc_time = datetime.now(timezone.utc)
ist_time = utc_time.astimezone(timezone(timedelta(hours=5, minutes=30)))
print("UTC:", utc_time)
print("IST:", ist_time)✔ Converts UTC → any timezone
✔ Useful for global applications
9. Getting Unix Timestamp ⏱️
timestamp_example.py
ts = datetime.datetime.now().timestamp()
print(ts)10. Create datetime from timestamp 🕰️
from_timestamp.py
dt = datetime.datetime.fromtimestamp(1700000000)
print(dt)11. Replace Parts of a datetime 🛠️
replace_example.py
now = datetime.datetime.now()
new_dt = now.replace(year=2030, month=12, day=25)
print(new_dt)12. Get Weekday Information 📆
weekday_example.py
import datetime
today = datetime.date.today()
print(today.weekday()) # 0 = Monday
print(today.isoweekday()) # 1 = MondayBest Practices 💡
- ✔ Use datetime for timestamps
- ✔ Use strftime & strptime for formatting & parsing
- ✔ Always work in UTC internally
- ✔ Convert to local timezone only when needed
Conclusion 🎉
>>“Datetime gives Python the power to work with real-world time — elegantly and accurately.” ✨
You now fully understand Datetime in Python! Want the next topic? Try Timezones, Calendar Module, Timers, or Date Arithmetic Projects. Just tell me! 😊