Sometimes, we want to convert datetime object to a string of date only in Python.
In this article, we’ll look at how to convert datetime object to a string of date only in Python.
How to convert datetime object to a string of date only in Python?
To convert datetime object to a string of date only in Python, we can use the datetime strftime method.
For instance, we write
import datetime
t = datetime.datetime(2022, 2, 23, 0, 0)
s = t.strftime('%m/%d/%Y')
to create a datetime object with
t = datetime.datetime(2022, 2, 23, 0, 0)
Then we call strftime with the datetime format we want to return the string as to return a datetime string with the specified format.
%m is the 2 digit month.
%d is the 2 digit date.
And %Y is the 4 digit year.
Conclusio
To convert datetime object to a string of date only in Python, we can use the datetime strftime method.