Python datetime timedelta

In this Python datetime tutorial, you will learn how to use the Python datetime timedelta function.

# import datetime module
import datetime

# create a datetime object
var_date = datetime.datetime(2020, 4, 21)

# print the var_date
print("var_date =", var_date)

# print the type of var_date
print("type of var_date =", type(var_date))

# add 5 days to var_date
var_date = var_date + datetime.timedelta(days=5)

# print the var_date
print("var_date =", var_date)

Output:

var_date = 2020-04-21 00:00:00
type of var_date = <class 'datetime.datetime'>
var_date = 2020-04-26 00:00:00

Free resources to learn advanced skills: AiHints and CodeAllow