Skip to main content

Working with datetime & timedelta

Whether you are building an e-commerce order tracker, calculating loan interest intervals, or computing the exact date when a user's subscription expires — working with dates and times is a mandatory skill for every software engineer.

Python provides the built-in datetime standard library to make date arithmetic safe and easy without worrying about leap years, 28/30/31-day month boundaries, or timestamps.


1. Getting Current Date & Time

The datetime class inside the datetime module gives you the current clock time down to the microsecond:

from datetime import datetime, date

# 1. Current Date and Time
now = datetime.now()
print("Current timestamp:", now)
# Output: Current timestamp: 2026-08-08 13:45:10.123456

# 2. Accessing Individual Components
print(f"Year: {now.year}, Month: {now.month}, Day: {now.day}")
print(f"Hour: {now.hour}, Minute: {now.minute}, Second: {now.second}")

# 3. Only Date (Without Time)
today = date.today()
print("Today's date:", today) # Output: Today's date: 2026-08-08

2. Creating Custom Date Objects

You can construct any past or future date by passing (year, month, day, hour, minute, second):

from datetime import datetime

# India Independence Day: 15 August 1947, 00:00:00
independence_day = datetime(1947, 8, 15, 0, 0, 0)
print(independence_day) # Output: 1947-08-15 00:00:00

3. Date Arithmetic with timedelta

A timedelta represents a duration — the difference between two dates or a period of time (e.g. 30 days, 2 hours, 15 minutes).

Real-World Use Case: Subscription Expiry

from datetime import datetime, timedelta

current_time = datetime.now()

# Add a 30-day Premium Plan duration
subscription_validity = timedelta(days=30)
expiry_date = current_time + subscription_validity

print("Purchase Date:", current_time.strftime("%Y-%m-%d"))
print("Plan Expiry Date:", expiry_date.strftime("%Y-%m-%d"))

Real-World Use Case: Calculating Days Between Two Dates

from datetime import date

birthday = date(2002, 5, 20)
today = date.today()

difference = today - birthday
print(f"You have been alive for {difference.days} days!")

4. Comparing Dates

Dates in Python can be compared naturally using standard comparison operators (<, >, ==):

from datetime import date

event_date = date(2026, 12, 31)
today = date.today()

if event_date > today:
print("🗓️ Event is in the future.")
elif event_date == today:
print("🎉 Event is happening today!")
else:
print("⌛ Event has already passed.")

Quick Summary

  • datetime.now() & date.today(): Capture current system date and time stamps.
  • timedelta(days=N, hours=H): Performs duration math (e.g., calculating expiration dates or past activity).
  • Comparison Operators: Date and datetime objects support standard <, >, == comparisons directly.

What's Next?

Let's learn how to format date objects into strings and parse user inputs with Date Formatting with strftime() & strptime()!