Convert datetime Object to Date Only String in Python (Example)
In this tutorial you’ll learn how to convert a datetime object to a string containing only the date in Python programming.
The table of content is structured as follows:
Let’s dive straight into the Python code!
Import datetime Module & Create Example Data
First, we have to import the datetime module into Python:
import datetime
Next, we can create a data object containing the current date and time using the following code:
data = datetime.datetime.now() print(data) # 2021-11-19 11:59:01.469718
As you can see, our example datetime object contains the 19th of November 2021 with the time 11:59:01.
Let’s return only the date of this datetime object as a character string!
Example 1: Extract Only Date from datetime Object Using strftime() Function
This example explains how to convert a datetime object to a date string using the strftime() function.
For this task, we can use the Python code below:
data.strftime('%m/%d/%Y') # '11/19/2021'
As you can see, we have returned only the date from our datetime object. Looks good!
However, the Python programming languages provides many different alternatives for this task. So let’s move on to the other examples…
Example 2: Extract Only Date from datetime Object Using %s Operator
This example shows how to use the %s operator and the month, day, and year attributes to display only the date from a datetime object:
'%s/%s/%s' % (data.month, data.day, data.year) # '11/19/2021'
The output is the same as in the previous example. However, this time we have used the %s operator instead of the strftime function.
Example 3: Extract Only Date from datetime Object Using date() Function
In this example, we’ll use the date() function along with the str() function to get a date in string format.
Consider the following Python code:
str(data.date()) # '2021-11-19'
Video, Further Resources & Summary
Would you like to learn more on dates and times in Python? Then you could have a look at the following YouTube video of Pretty Printed’s YouTube channel.
In the video, the speaker explains how to parse and format dates in Python:
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
Furthermore, you could have a look at some of the other tutorials on Statistics Globe:
- Handling DataFrames Using the pandas Library in Python
- Keep Only Date Part when Using pandas.to_datetime in Python
- Convert datetime Object to Date & Vice Versa in Python
- Convert datetime into String with Milliseconds in Python
- Convert datetime to String without Microsecond Component
- Convert String to datetime Object in Python
- pandas DataFrames Operations in Python
- How to Modify & Edit a pandas DataFrame in Python
- Python Programming Tutorials
This post has shown how to convert a datetime object to a date only character string using the Python programming language. In case you have further questions, you may leave a comment below.
This tutorial was created in collaboration with Gottumukkala Sravan Kumar. Please have a look at Gottumukkala’s author page to get more information about his academic background and the other articles he has written for Statistics Globe.