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:

 

 

Furthermore, you could have a look at some of the other tutorials on Statistics Globe:

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.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top