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:
2024-12-03 07:10:11.046848[Finished in 0.012393760960549116s]

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.0118428620044142s]

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.011559050064533949s]

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:
626 days, 7:10:11.086279[Finished in 0.011827399954199791s]

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: 2024-12-03 07:10:11.099496yesterday:  2024-12-02 07:10:11.099496[Finished in 0.012021194910630584s]

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:
2024-11-26 07:10:11.112804[Finished in 0.012144943000748754s]

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:  Tue Dec  3 07:10:11 2024 tomorrow:  Wed Dec  4 07:10:11 2024[Finished in 0.011967575876042247s]
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:  Tue Dec  3 07:10:11 2024 next week:  Tue Dec 10 07:10:11 2024[Finished in 0.011946656974032521s]

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:
FalseTrueTrue[Finished in 0.012113532051444054s]