Skip to content
Home » Convert String to Datetime in Python

Convert String to Datetime in Python

In this tutorial let’s learn about How to Convert String to Datetime in Python. For dealing with dates and times, Python has the datetime module. As you might expect, it includes a number of functions for manipulating dates and timings. We can easily parse any date-time text and convert it to a datetime object using this module.

Convert String to Datetime using datetime.strptime() in Python

Python’s datetime class includes the strptime() function for converting a string representation of the date/time into a date object. strptime() is a date-time conversion function present in the datetime and time modules. This function converts the given datetime string to the specified format.

Syntax for strptime() method :

datetime.strptime(date_string, format)

Code Example :

import datetime

date_time_string = '2022-01-31 10:12:30.1432'
date_time_obj = datetime.datetime.strptime(date_time_string, '%Y-%m-%d %H:%M:%S.%f')

print('Date:', date_time_obj.date())
print('Time:', date_time_obj.time())
print('Date-time:', date_time_obj)

Output:

Date: 2022-01-31
Time: 10:12:30.143200
Date-time: 2022-01-31 10:12:30.143200

In this example, we’re going to use a new method called strptime(). This method accepts two arguments: the string representation of the date and time, and the format of the input string. Specifying the format in this manner speeds up parsing. The returned value is of the datetime type.

In our example, the input string is “2022-01-31 10:12:30.1432,” and the format of our date string is “% Y- % m- % d % H: % M: % S. % f.” The datetime value returned is saved in the date_time_obj variable. Because this is a datetime object, we can easily call the date() and time() methods on it. The output shows that it outputs the ‘date’ and ‘time’ parts of the input text.

Examples to Convert String to Datetime in Python

Example 1:

import datetime

def convert(date_time):
	format = '%b %d %Y %I:%M%p'
	datetime_str = datetime.datetime.strptime(date_time, format)

	return datetime_str

date_time = 'Feb 3 2022 11:17AM'
print(convert(date_time))

Output:

2022-02-03 11:17:00

Example 2:

from datetime import datetime

date_time_str = '1/08/22 10:12:30'

date_time_obj = datetime.strptime(date_time_str, '%d/%m/%y %H:%M:%S')


print ("The type of the date is now",  type(date_time_obj))
print ("The date is", date_time_obj)

Output:

The type of the date is now <class ‘datetime.datetime’>
The date is 2022-08-01 10:12:30

Example 3:

from datetime import datetime

dateString = "Wednesday, August 18, 2022 10:12:30"
dateFormatter = "%A, %B %d, %Y %H:%M:%S"
datetime.strptime(dateString, dateFormatter)

Output:

datetime.datetime(2022, 8, 18, 10, 12, 30)

Conclusion

In this tutorial we learnt different ways to convert string into datetime format using strptime() method.

Similar Posts:

How to Extract numbers from a String in Python
Remove Spaces From a String in Python
Convert a String to Variable Name in Python