Python datetime to date

In this Python datetime tutorial, you will learn how to convert Python datetime to date.

# import datetime module
import datetime

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

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

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

# convert datetime to date
var_date = var_date.date()

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

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

Output:

var_date = 2020-04-21 02:45:30
type of var_date = <class 'datetime.datetime'>
var_date = 2020-04-21
type of var_date = <class 'datetime.date'>

Free resources to learn advanced skills: AiHints and CodeAllow