Convert String to datetime Object in Python (3 Examples)

This tutorial shows how to convert a string to a datetime object in the Python programming language.

Table of content:

Let’s do this!

Import datetime Module & Create Example Data

As a first step, we have to import the datetime module to Python:

from datetime import datetime

Next, we have to create a string containing a date and a time that we can use in the examples later on:

my_string = '05/25/21 01:22:06'
print(my_string)
# 05/25/21 01:22:06

The previous output shows our example string. It contains the date and time 05/25/21 01:22:06.

 

Example 1: Convert String to datetime Object

This example uses the strptime() function of the datetime module. This function takes the date string (month, day, and year) as input and returns it as a datetime object.

my_datetime = datetime.strptime(my_string, '%m/%d/%y %H:%M:%S')
print(my_datetime)
# 2021-05-25 01:22:06

The previous Python code has created a new datetime object containing the date and time of our input string.

 

Example 2: Extract Only Date from String

We can also use the strptime() function to return only the date from a character string.

For this, we have to use the date() function in addition to the code that we have used in Example 1:

my_date = datetime.strptime(my_string, '%m/%d/%y %H:%M:%S').date()
print(my_date)
# 2021-05-25

As you can see, we have only printed the date component of our input string.

 

Example 3: Extract Only Time from String

Similar to Example 2, we can apply the time() function to get only the time of a string with date and time components.

Consider the Python syntax and its output below:

my_time = datetime.strptime(my_string, '%m/%d/%y %H:%M:%S').time()
print(my_time)
# 01:22:06

 

Video, Further Resources & Summary

If you need more explanations on how to transform strings to datetime objects in Python, you can watch the following video of the PyLenin YouTube channel.

In the video, the speaker explains how to use strptime and strftime of the datetime module for converting strings to datetime objects and the other way around.

 

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.

YouTube Content Consent Button Thumbnail

YouTube privacy policy

If you accept this notice, your choice will be saved and the page will refresh.

 

Furthermore, you may have a look at some other tutorials on the Statistics Globe website:

Summary: This post has illustrated how to change the string class to a datetime object in the Python programming language. For additional questions on data types and datetime objects, please leave a comment below.

 

Gottumukkala Sravan Kumar Statistician & Programmer

Note: This article was created in collaboration with Gottumukkala Sravan Kumar. You may find more information about Gottumukkala and his other articles on his profile page.

 

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.

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

Menu
Top