The datetime class in the datetime module provide a structured approach in  managing date and time data. It provides a wide range of tools and methods that enable easy manipulation of  dates and time values.

ExampleEdit & Run
from datetime import datetime

print(datetime.now())
Output:
2025-03-20 10:24:11.657689 [Finished in 0.012647390365600586s]

In the above example, we used the now()  class method to get the current date and time values. 

ExampleEdit & Run
from datetime import datetime

pi_day = datetime(year = 2023, month = 3, day = 14, hour = 11, minute = 30, second = 30, microsecond = 500)

print(pi_day)
Output:
2023-03-14 11:30:30.000500 [Finished in 0.012204953003674746s]

Timedelta objects, on the other hand, are used to represent a duration in terms of days, seconds, and microseconds.    

ExampleEdit & Run
from datetime import timedelta

td = timedelta(days=5, seconds=120, microseconds=50000, milliseconds=3000, minutes=10, hours=3, weeks=1)

print(td)
Output:
12 days, 3:12:03.050000 [Finished in 0.011904439888894558s]

A timedelta can be defined as a difference between two dates or datetime objects. 

Performing arithmetic operations on datetimes

We can use the standard operators on datetime objects. For example we can add two time to a objects together, subtract one from the other, compare two of them using comparison operators such as '<', '>', and '==', and access their individual components such as year, month, and day using dot notation.

Subtraction

ExampleEdit & Run
from datetime import datetime

today = datetime.now()

past = datetime(2023, 3, 18)

#This returns a timedelta object
print(today - past)
Output:
733 days, 10:24:11.697788 [Finished in 0.014077897183597088s]

Whenever we subtract two datetime objects, as in above, a timedelta is returned representing the time between the two days. We can also similarly subtract a timedelta from a datetime to get an earlier datetime.

ExampleEdit & Run
from datetime import datetime, timedelta

today = datetime.now()
one_day = timedelta(days = 1)


yesterday = today - one_day
print('today:', today)

print('yesterday: ', yesterday)
Output:
today: 2025-03-20 10:24:11.712933 yesterday: 2025-03-19 10:24:11.712933 [Finished in 0.014707411639392376s]

In the above example we instantiated a timedelta object which holds a duration of one day. On subtracting now with the timdelta we get yesterday's datetime object.

ExampleEdit & Run
from datetime import datetime, timedelta

today = datetime.now()
one_week = timedelta(weeks = 1)

last_week = today - one_week

print(last_week)
Output:
2025-03-13 10:24:11.728512 [Finished in 0.014951866120100021s]

Addition

When we add two datetime objects, the result will be a timedelta object representing the duration of time between the two datetime objects. We can even more usefully add a timedelta object to a datetime object, and the result will be a new datetime object that is the original datetime plus the timedelta duration.

ExampleEdit & Run
from datetime import datetime, timedelta

today = datetime.now()

one_day = timedelta(days = 1)

tomorrow = today + one_day
print('today: ', today.ctime(), '\n')
print('tomorrow: ', tomorrow.ctime())
Output:
today: Thu Mar 20 10:24:11 2025 tomorrow: Fri Mar 21 10:24:11 2025 [Finished in 0.012330914847552776s]
ExampleEdit & Run
from datetime import datetime, timedelta

today = datetime.now()

one_week = timedelta(weeks = 1)

next_week = today + one_week
print('today: ', today.ctime(), '\n')
print('next week: ', next_week.ctime())
Output:
today: Thu Mar 20 10:24:11 2025 next week: Thu Mar 27 10:24:11 2025 [Finished in 0.011174402199685574s]

Comparing datetimes

We can use comparison operators to compare datetime objects  in order to get the relation between them.

ExampleEdit & Run
from datetime import datetime, timedelta

one_day = timedelta(days = 1)
two_days = timedelta(days = 2)

today = datetime.now()
yesterday = today - one_day
tomorrow = today + one_day


#use the operators
print(today > tomorrow)
print(today > yesterday)
print(yesterday == (tomorrow - two_days) )
Output:
False True True [Finished in 0.011726509779691696s]