Converting String into Datetime | Python

Rate this post
  • Summary: Methods used to covert datetime strings to datetime objects –
    • datetime.striptime()
    • parser.parse()
    • datetime.fromisoformat()

Problem Statement: Given a list of date-time strings in Python; how to convert the strings into datetime format?

Video Walkthrough

Example: Consider that you have a list as shown below, having date-time as strings. You want to convert them from a string object to a date-time object so that you can store them in the database properly.

from datetime import datetime

dt = ['Jan 1 2005  2:35PM',
      'Jul 5 2005  06:43AM',
      'Aug 21 1993 12:25PM',
      'Dec 28 2000 08:00AM']
for count, i in enumerate(dt):
    print(f"datetime[{count+1}]: {i}, type= {type(i)}")

Output:

datetime[1]: Jan 1 2005  2:35PM, type= class 'str'>
datetime[2]: Jul 5 2005  06:43AM, type= class 'str'>
datetime[3]: Aug 21 1993 12:25PM, type= class 'str'>
datetime[4]: Dec 28 2000 08:00AM, type= class 'str'>

Expected Output:

datetime[1]: 2005-01-01 14:35:00, type= <class 'datetime.datetime'>
datetime[2]: 2005-07-05 06:43:00, type= <class 'datetime.datetime'>
datetime[3]: 1993-08-21 12:25:00, type= <class 'datetime.datetime'>
datetime[4]: 2000-12-28 08:00:00, type= <class 'datetime.datetime'>

Tidbit

Python enumerate()

We have a clear idea about the problem at hand. Now, let us dive into the solutions to our mission-critical question.

Method 1: Using datetime.strptime()

Approach: A quick solution to parse strings into datetime objects is to use strptime method of Python’s datetime module.

Here’s a quick tip for you to remember this –

strptime = "string parse time"

You can learn more about this method here – strptime()

Code: Let’s visualize the solution with the help of the following code.

from datetime import datetime

dt = ['Jan 1 2005  2:35PM',
      'Jul 5 2005  06:43AM',
      'Aug 21 1993 12:25PM',
      'Dec 28 2000 08:00AM']
print("Before conversion:")
for count, i in enumerate(dt):
    print(f"datetime[{count+1}]: {i}, type= {type(i)}")
obj_dt = []
for i in dt:
    obj_dt.append(datetime.strptime(i, '%b %d %Y %I:%M%p'))
print("\nAfter Converting String to Datetime: ")
for count, i in enumerate(obj_dt):
    print(f"datetime[{count+1}]: {i}, type= {type(i)}")

Output:

Before conversion:
datetime[1]: Jan 1 2005  2:35PM, type= <class 'str'>
datetime[2]: Jul 5 2005  06:43AM, type= <class 'str'>
datetime[3]: Aug 21 1993 12:25PM, type= <class 'str'>
datetime[4]: Dec 28 2000 08:00AM, type= <class 'str'>

After Converting String to Datetime: 
datetime[1]: 2005-01-01 14:35:00, type= <class 'datetime.datetime'>
datetime[2]: 2005-07-05 06:43:00, type= <class 'datetime.datetime'>
datetime[3]: 1993-08-21 12:25:00, type= <class 'datetime.datetime'>
datetime[4]: 2000-12-28 08:00:00, type= <class 'datetime.datetime'>

If in case you only need the date instead of datetime, here’s the code to do that:

d = 'Jan 1 2005  2:35PM'
print(datetime.strptime(d, '%b %d %Y %I:%M%p').date())

# 2005-01-01

Method 2: Using dateutil library

If you have the freedom to use third-party module, then dateutil library might just be the perfect solution to this problem. It is a powerful extension that enhances the abilities o the standard datetime module in Python.

Since it is a third-party module, you need to inntall it using: pip install python-dateutil

Now, let’s have a look at the solution. Please follow the comments in the given code to understand how it is working.

from dateutil import parser

dt = ['Jan 1 2005  2:35PM',
      'Jul 5 2005  06:43AM',
      'Aug 21 1993 12:25PM',
      'Dec 28 2000 08:00AM']
print("Before conversion:")
for count, i in enumerate(dt):
    print(f"datetime[{count+1}]: {i}, type= {type(i)}")

print("After Conversion: ")
dt_obj = []
for i in dt:
    dt_obj.append(parser.parse(i))
for count, i in enumerate(dt_obj):
    print(f"datetime[{count+1}]: {i}, type= {type(i)}")

Output:

Before conversion:
datetime[1]: Jan 1 2005  2:35PM, type= <class 'str'>
datetime[2]: Jul 5 2005  06:43AM, type= <class 'str'>
datetime[3]: Aug 21 1993 12:25PM, type= <class 'str'>
datetime[4]: Dec 28 2000 08:00AM, type= <class 'str'>

After Converting String to Datetime: 
datetime[1]: 2005-01-01 14:35:00, type= <class 'datetime.datetime'>
datetime[2]: 2005-07-05 06:43:00, type= <class 'datetime.datetime'>
datetime[3]: 1993-08-21 12:25:00, type= <class 'datetime.datetime'>
datetime[4]: 2000-12-28 08:00:00, type= <class 'datetime.datetime'>

Discussion: Though this is an effective way to convert datetime string to datetime object, it has a drawback when it comes to time consumed by the script. parser is slower than strptime! However, if you are not dealing with millions of timestamps at a time then using parser can be more convenient.

Here’s a comparison of the two methods –

from dateutil import parser
from datetime import datetime
import timeit

def dt_parse_converter():
    dt = parser.parse("Jun 1 2005  1:33PM")
def strptime_converter():
    datetime_object = datetime.strptime('Jun 1 2005  1:33PM', '%b %d %Y %I:%M%p')

print(timeit.timeit(dt_parse_converter, number=10**5))
print(timeit.timeit(strptime_converter, number=10**5))

Output:

13.0287009
1.8845770999999996

Method 3: Using datetime.fromisoformat

If you are working on Python 3.7 or above you can use the fromisofromat method of the datetime module which allows us to convert datetime string in YYYY-MM-DD format to a datetime object.

Code:

from datetime import datetime

# given datetime strings in the list
dt = ['Jan 01 2012  10:10 PM',
      'Jul 05 2005  06:43 AM',
      'Aug 21 1993 12:25 PM',
      'Dec 28 2000 08:00 AM']
print("Before conversion:")
for count, i in enumerate(dt):
    print(f"datetime[{count + 1}]: {i}, type= {type(i)}")

print()
print("After Conversion: ")
# month dictionary stores months and their numeric equivalents in key-value pairs
month = {
    'Jan': '01', 'Feb': '02', 'Mar': '03', 'Apr': '04',
    'May': '05', 'Jun': '06', 'Jul': '07', 'Aug': '08',
    'Sept': '09', 'Oct': '10', 'Nov': '11', 'Dec': '12',
}
dt_obj = []  # stores the finally converted date-time objects
for i in dt:
    month_str = i.split(' ')[0] # extracts month string from date string
    month_num = i.replace(month_str, month[month_str]) # replaces month string with month numeric value
    # converting month-dd-yyyy to yyyy-mm-dd
    k = str(
        month_num.split(' ')[2] + "-" + month_num.split(' ')[0] + "-" + month_num.split()[1] + " " + month_num.split()[
            3] + ":00")
    # appending each datetime object after converting string to datetime object using fromisoformat()
    dt_obj.append(datetime.fromisoformat(k))
# prints the output
for count, i in enumerate(dt_obj):
    print(f"datetime[{count + 1}]: {i}, type= {type(i)}")

Output:

Before conversion:
datetime[1]: Jan 01 2012  10:10 PM, type= <class 'str'>
datetime[2]: Jul 05 2005  06:43 AM, type= <class 'str'>
datetime[3]: Aug 21 1993 12:25 PM, type= <class 'str'>
datetime[4]: Dec 28 2000 08:00 AM, type= <class 'str'>

After Conversion: 
datetime[1]: 2012-01-01 10:10:00, type= <class 'datetime.datetime'>
datetime[2]: 2005-07-05 06:43:00, type= <class 'datetime.datetime'>
datetime[3]: 1993-08-21 12:25:00, type= <class 'datetime.datetime'>
datetime[4]: 2000-12-28 08:00:00, type= <class 'datetime.datetime'>

Conclusion

Phew! That was some journey, but we have conquered datetime strings and successfully converted them to datetime objects. We also saw the advantages and disadvantages of using parser() over simple striptime(). Feel free to use any of the methods that solve your purpose. In case you have any doubt, drop in a comment and we will answer your queries.

Please subscribe and stay tuned for more interesting articles and discussions. Happy learning!


Recommended: Finxter Computer Science Academy

  • One of the most sought-after skills on Fiverr and Upwork is web scraping. Make no mistake: extracting data programmatically from websites is a critical life skill in today’s world that’s shaped by the web and remote work.
  • So, do you want to master the art of web scraping using Python’s BeautifulSoup?
  • If the answer is yes – this course will take you from beginner to expert in Web Scraping.