Convert "unknown format" strings to datetime objects in Python


Dates can be in many formats like: “2009/05/13 19:19:30”, “May 13 2009 07:19PM", and “2009-05-13 19:19”.

Python provides many modules to work with data related to date times. To read an unknown format of date strings into a python datetime object, we can use the python dateutil, datetime modules.

The python datetime object is a single object that has all the information about the date and time objects. It represents the data related to the year, month, day, hours, mintunes, seconds, and time zones also.

In this article below, we will see how to convert the unknown format strings to python datetime object. Let’s see the input-output scenarios to understand convert an unknown format string to datetime objects in Python.

Input Output Scenarios

Assume we have input string of unknown format. And in the output, we can see converted datetime object.

Input string (unknown format):
20050607T090650

Output Datetime object:
2005-07-06 09:06:50
Data type: 

Using datetime. strptime() method

We can use the datetime library to convert a date time string into a datetime object. The method strptime() parse a string into a datetime object corresponding to the given format codes. Following is the syntax of this method –

datetime.strptime(data, format_data) 

Where,

  • Data: date_string.

  • Format: converts the date_string based on this format codes.

A few format Codes are

  • %a Weekday as the abbreviated name.

  • %A Weekday as full name.

  • %w Weekday as a decimal number.

  • %d Day of the month with leading zero.

  • %B Month as full name.

  • %b Month as the abbreviated name.

  • %m Month as leading zero.

  • %y year in two-digit representation.

  • %Y year with century.

  • %H Hour (24-hour clock) as a zero-padded decimal number.

  • %M Minute as a zero-padded decimal number.

  • %S Second as a zero-padded decimal number.

  • %z UTC offset in the form ±HHMM[SS[.ffffff]].

  • %Z Time zone name.

Example

We will convert the dates in string data into the datetime object by using strptime() method.

from datetime import datetime
date_string = '20151002151700-0800'
print("Input string (unknown format):")
print(date_string)
obj = datetime.strptime(date_string, "%Y%m%d%H%M%S%z")
print("Output Datetime object:")
print(obj)
print('Data type:',type(obj))

Output

Input string (unknown format):
20151002151700-0800
Output Datetime object:
2015-10-02 15:17:00-08:00
Data type: <class 'datetime.datetime'>

In the above block we can see the converted datetime object from the input string. We have specified the format "%Y%m%d%H%M%S%z" to the strptime() method.

Example

Let’s take another example and convert the date string into the datetime object by specifying the format code.

from datetime import datetime

date_string = 'Wed Mar 16 16:12:05 2016 +0800'
print("Input string (unknown format):")
print(date_string)

obj = datetime.strptime(date_string, '%a %b %d %H:%M:%S %Y %z')
print("Output Datetime object:")
print(obj)
print('Data type:',type(obj))

Output

Input string (unknown format):
Wed Mar 16 16:12:05 2016 +0800
Output Datetime object:
2016-03-16 16:12:05+08:00
Data type: <class 'datetime.datetime'>

Here "Wed Mar 16 16:12:05 2016 +0800" is the string and obj is a datetime object.

Using dateutil.parser.parse() function

The parse() method of dateutil module is very flexible and will parse anything in the given string data. The parse function offers more formats to parse a date string into a datetime object.

Example

Here we will use the parse() method to read the string data of unknown format to datetime object.

from dateutil.parser import parse

date_string = 'Wed Mar 16 16:12:05 2016 +0800'
print("Input string (unknown format):")
print(date_string)

obj = parse(date_string)
print("Output Datetime object:")
print(obj)
print('Data type:',type(obj))

Output

Input string (unknown format):
Wed Mar 16 16:12:05 2016 +0800
Output Datetime object:
2016-03-16 16:12:05+08:00
Data type: <class 'datetime.datetime'>

The converted datetime object from the unknown format is displayed in the above block.

Example

Note that some date_strings can be ambiguous, for example: 1995-06-07 could mean June 7 or July 6. The parse method has parameters like dayfirst and yearfirst to handle this type ambiguous data.

from dateutil.parser import parse

date_string = '20050607T090650'
print("Input string (unknown format):")
print(date_string)

obj = parse(date_string, dayfirst=True)
print("Output Datetime object:")
print(obj)
print('Data type:',type(obj))

Output

Input string (unknown format):
20050607T090650
Output Datetime object:
2005-07-06 09:06:50
Data type: <class 'datetime.datetime'>

In this example, we have specified the Boolean Value True to the dayfirst parameter, so in the output the datetime object represents day first (june 7).

Updated on: 30-May-2023

852 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements