Register Login

Converting string into datetime in Python

One of the most critical issues software developers face while creating applications is dealing with data and time. Many of them get confused about what format these data and time should be taken from the user or what data type will best suit such data storage. But if you are a Python developer, you do not have to worry about it at all.

Most APIs render date and time in the form of a string. To convert such date and time-based string data into a human-readable format for the application, we need to convert it. Again, there is a factor of time zone. Every API reflects different time values for different time zones. To keep this conversion standardized, a good date-time module can help. In this article, you will learn about a Python library that can help in converting your string-based date and time data into a proper format.

Method 1: Using the datetime Module:

Python's built-in datetime module allows us to deal with dates and times. Also, you can guess that like other modules, this will also have some predefined methods that will make our tasks easy.

Python's datetime module has three different object types: the date, the time, and both together as datetime. The date object holds the date so as the time object, and datetime holds both.

strptime(): The datetime and time sub-modules contains the definition of strptime() that will be responsible for Date-Time Conversion from string format. It automatically changes any specified string that contains date & time in string into the desired format.

The syntax is:

datetime.strptime(string_of_date, specified_format)

where, the string_of_date is the string that will be passed to convert it to date-time format. This argument has to be in string type only. Again, the specified_format is the format in which it will return or display the date and time (for example: YYYY-MM-DDTHH:MM:SS.mmmmmm).
The default format used in it is the ISO 8601 format i.e. "2021-08-28 08:15:27.245680".

If the input string is set/written in the same ISO 8601 format, this method can easily detect and parse it to a datetime object.


import datetime
# Function to convert string to datetime
def converter(dt):
format = '%b %d %Y %I:%M%p'  # You have to specify the format either statically or dynamically
datetime_str = datetime.datetime.strptime(dt, format)
return datetime_str

DateTime = 'Aug 28 2021 02:06AM'
print('String: ', DateTime)
print('Date Time: ',converter(DateTime))


String:  Aug 28 2021 02:06AM
Date Time:  2021-08-28 02:06:00


First, we have imported the datetime module. Then, we have created a converter function that accepts one parameter. Inside the function, we have used the format object where we specified the date and time format statically. Then we take the string and the format as an argument within the datetime.strptime() method and return the object holding the converted value.

Outside the function, we have statically added a string that contains our date and time information and passed it as the parameter within the converter() user-defined function.

Method 2: Using the dateutil Module:

The dateutil module is an extension of the date and time module. It is advantageous in the sense that developers do not need to pass any parsing code to the input string.

It has a parse() function that will automatically convert a string into date and time format. Also, it does not take the string format as parameter which makes it easy to work with.




from dateutil import parser
dateString = "2021/8/28 02:00:06"
print('Date String: ', dateString)

dt = parser.parse(dateString, dayfirst = True)
print('Date Time: ',dt)


Date String:  2021/8/28 02:00:06
Date Time:  2021-08-28 02:00:06


Here, we will import the parser from the dateutil module. Then we will assign a string to the dateString object that will hold our date and time information. Then, we will use the parse() method and pass the date information and use the other default parameter as dayfirst = True. Finally, we have used the print() function to display the converted date and time.


Both datetime and dateutil modules are a great way of converting strings into date and time format. But the datetime module allows a more detailed way of representing date and time. We can set out custom formats that have international standards.