Python String to Date

Handling dates and timings are one of the many typical issues we confront in software development. For example, after receiving a date-time string from an API, we must convert it to a human-readable format. The conversion will be different if the same API is used in different time zones. The time should be transformed conferring to the timezone in a decent date-time library. This is only one of the numerous considerations that must be made when working with dates and times.

Fortunately, Python has a built-in module for functioning with dates and times called datetime. It has several functions for operating dates and times, as you might expect. We can easily parse any date-time text and convert it to a datetime object using this module. To convert a string to a datetime or time object, use the strptime() method from the datetime and time modules. Let’s have a look at the illustrations underneath to understand how you can achieve it.

Example 1

We are going to use a new method called strptime in this example. This function requires two arguments: the first represents the date-time string, and the second is the input string format. Datetime does not have to try to comprehend the format on its own, which is significantly more computationally expensive. By specifying the format in this way, the parsing is much faster. The type of return value is datetime “07:10:25.127650,” and the format of our date string is “%Y-%m-%d %H:%M:%S.%f”.

The input string in our example is “2015-05-20 datetime value returned is saved in the date_time_myobj variable. We can use the date() and time() methods directly on this datetime object because it is a datetime object. The appended code will be executed in the spyder tool.

import datetime
date_time_mystr="2015-05-20 07:10:25.127650"
date_time_myobj=datetime.datetime.strptime(date_time_mystr, '%Y-%m-%d %H:%M:%S.%f')
print('Date is:',
print('Time is:' ,
print('Date and Time is:',date_time_myobj)

Now, this code is ready to be executed in the spyder tool. The ‘date’ and ‘time’ parts of the input string are printed, as you can see in the result.

Note: You might be curious as to what the format “%Y-%m-%d %H:%M:%S.%f” means. Format tokens are what they’re called. Each token denotes a particular aspect of the date-time, such as the day, month, or year.

Example 2

In the next example, we will use Python to convert a string to a datetime pandas object. There is a module called pandas. We will import pandas as pp in this case. The datetime(ab) method in Python is used to convert a text datetime into a datetime object. Print( datetime(ab)) is used to receive the output as a datetime object. The appended code will be executed in the spyder tool.

import pandas as pp
ab = [20-10-2105 5:40:00 Pm’]

This code is ready to be executed in the spyder tool. Refer to the subsequent affixed screenshot to view the output.

Example 3

We can now check how to alter a string to a datetime with a timezone in programming languages like Python. We have imported a module called timezone in this example. To get the current time with timezone, use‘UTC’)). time = “%Y-%m-%d %H:%M:%S%Z%z” is the format. The percent z is used to calculate the timezone as well as the datetime. The appended code will be executed in the spyder tool.

from datetime import datetime
from pytz import timezone
t="%Y_%m_d %H:%M:%S%Z%z"'UTC'))

Now, this code is ready to be executed in the spyder tool. print(‘UTC :’, t) is used to receive the output. The outcome can be seen in the screenshot displayed underneath. 


You have now learned about dates. This is a unique data type. The values appear to be strings, but they have properties such as determining the number of days between two dates, determining whether a date is greater than others, and so on. The datetime library is a popular Python module for manipulating dates. The datetime library’s strptime() method is used to translate a string input into a date. The above examples illustrate the several forms that are routinely used. Now you know how to make use of the datetime strptime function in Pandas, as well as what datetime format codes are and how to convert string to date in Python.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content