Convert String to DateTime Object in Python

Convert String to DateTime Object in Python

Answer: You can use datetime.strptime() function to convert the string to datetime object in Python.

In Python, conversion of string to datetime objects is required when we are working with dates and times in a specified format. There are multiple ways in Python to convert a string to datetime object. In this blog, you will learn various approaches and understand them with examples.

Table of Contents:

Methods to Convert String to DateTime Objects in Python

Python provides various methods in its datetime module to convert strings into datetime objects. Some of these methods are explained below with the help of examples:

Method 1: Using datetime.strptime() to Convert Strings to DateTime in Python

The strptime() method converts a date string into datetime objects. It should have a specified format for the string.

Example:

# Import the datetime module from the datetime library
from datetime import datetime

# Use strptime to convert the string into a datetime by specifying the format
datetime = datetime.strptime('Jun 1 2005 1:33PM', '%b %d %Y %I:%M%p')

# Print the resulting datetime
print(datetime)

Output:

Explanation:

Here, the string “Jun 1, 2005, 1:33 PM” is converted into a datetime object using the format “%Y-%m-%d %H:%M”. This tells strptime() how to interpret the year, month, day, hour, and minute in the string.

Method 2: Using dateutil module to Convert Strings to DateTime in Python

The dateutil module consists of the parser.parse() function, which is a very useful method for converting the provided strings into datetime objects. It does this without asking for you to provide the input date and time format. 

Example:

# Import the 'parser' module from 'dateutil' library
from dateutil import parser

# Define a string representing a date and time
date_string = "March 5, 2022 11:30PM"

# Parse the string into a datetime
datetime = parser.parse(date_string)

# Print the parsed datetime 
print(datetime)

Output:

Explanation:

Parser.parse() automatically detects the month, day, year, and time from the string “March 5, 2022, 11:30 PM” and converts it into a datetime object.

Method 3: Using the pd.to_datetime() Function to Convert Strings to DateTime in Python

By utilizing the pd.to_datetime() function in the various pandas dataframes, you can convert a string into a datetime object. This method will convert different date-time formats.

Example:

# Import the 'parser' module from 'dateutil' library
from dateutil import parser

# Define a string representing a date and time
date_string = "March 5, 2022 11:30PM"

# Parse the string into a datetime
datetime = parser.parse(date_string)

# Print the parsed datetime 
print(datetime)

Output:

Explanation: 

This is the pd.to_datetime() function and automatically collects the string to the datetime format according to the right adjustment.

Method 4: Using time.strptime() to Convert String to DateTime in Python

It is similar to the strptime() method that works with the datetime module, the time.strptime() method works with the time module. This is normally used for simpler date situations.

Example:

# Import the time module
import time

# Define a string representing a date and time
date_string = "June 1 2025 13:33"

# Convert the string into a time object 
date_time = time.strptime(date_string, "%B %d %Y %H:%M")

# Use strftime to format the dates
datetime = time.strftime("%Y-%m-%d %H:%M:%S", date_time)

# Print the converted datetime
print(datetime)

Output:

Explanation:

The time.strptime() method works on the sample string “2025-01-30 13:33” and saves and returns time.struct_time object.

Conclusion

Python has multiple ways to convert a string to a datetime. The most commonly used is the simple datetime.strptime(), from which you get flexibility by using dateutil module. Understanding these methods helps you efficiently convert strings to a datetime object.

About the Author

Senior Consultant Analytics & Data Science

Sahil Mattoo, a Senior Software Engineer at Eli Lilly and Company, is an accomplished professional with 14 years of experience in languages such as Java, Python, and JavaScript. Sahil has a strong foundation in system architecture, database management, and API integration. 

Full Stack Developer Course Banner