Python - Date and Time
Working with date and time is very important in programming. Whether you are building websites, apps, or automation tools, you often need to handle current time, scheduling, logs, or time differences.
Python provides a built-in module called datetime to work with dates and times easily.
In this tutorial, you will learn how to use Python date and time features with simple examples.
What is Date and Time in Python?
Date and time in Python are handled using:
The
datetimemodule, which provides classes for working with dates, times, and time intervals.
Importing Date and Time Module
import datetimeGet Current Date and Time
import datetime
now = datetime.datetime.now()
print("Current Date and Time:", now)Output Example
Current Date and Time: 2026-06-08 13:45:12.123456Get Current Date Only
import datetime
today = datetime.date.today()
print("Today Date:", today)Get Current Time Only
import datetime
current_time = datetime.datetime.now().time()
print("Current Time:", current_time)Formatting Date and Time
We can format date and time using strftime().
Example
import datetime
now = datetime.datetime.now()
formatted = now.strftime("%Y-%m-%d %H:%M:%S")
print("Formatted:", formatted)Common Format Codes
| Code | Meaning |
|---|---|
| %Y | Year (2026) |
| %m | Month (01–12) |
| %d | Day (01–31) |
| %H | Hour (00–23) |
| %M | Minute |
| %S | Second |
Creating Custom Date
import datetime
d = datetime.date(2025, 12, 25)
print("Custom Date:", d)Creating Custom Date and Time
import datetime
dt = datetime.datetime(2025, 12, 25, 10, 30, 0)
print("Custom DateTime:", dt)Working with Time Difference
We use timedelta for calculations.
Example
import datetime
now = datetime.datetime.now()
future = now + datetime.timedelta(days=7)
print("After 7 days:", future)Time Difference Calculation
import datetime
start = datetime.datetime(2026, 1, 1)
end = datetime.datetime(2026, 1, 10)
diff = end - start
print("Difference:", diff.days, "days")What is timedelta?
timedelta represents:
The difference between two dates or times.
Real-World Example: Age Calculator
import datetime
birth_year = 2000
current_year = datetime.datetime.now().year
age = current_year - birth_year
print("Age:", age)Real-World Example: Countdown Timer
import datetime
import time
target = datetime.datetime(2026, 12, 31, 0, 0, 0)
while True:
now = datetime.datetime.now()
remaining = target - now
print("Time left:", remaining)
time.sleep(1)Working with Timestamps
import datetime
timestamp = datetime.datetime.now().timestamp()
print("Timestamp:", timestamp)Convert Timestamp to DateTime
import datetime
ts = 1700000000
dt = datetime.datetime.fromtimestamp(ts)
print("DateTime:", dt)Why Date and Time is Important
- Logging events
- Scheduling tasks
- User activity tracking
- File timestamps
- Notifications and reminders
Best Practices
1. Always use datetime module
Avoid manual date calculations.
2. Use strftime for formatting
Makes output readable.
3. Use timedelta for calculations
More reliable than manual math.
4. Be careful with timezones (advanced)
Use pytz or zoneinfo.
Common Mistakes
1. Confusing date and datetime
# date vs datetime are different2. Ignoring formatting
Raw datetime is hard to read.
3. Manual time calculations
Can lead to errors.
Summary
Python’s datetime module makes it easy to work with dates and times. You can get current time, format it, create custom dates, and perform calculations using timedelta.
Key Takeaways
- Use
datetimemodule for all time operations now()gives current date and timestrftime()formats outputtimedeltahandles time differences- Useful for real-world applications like scheduling and logging
Mastering date and time handling is essential for building real-world Python applications.


0 Comments