Convert “unknown format” strings to datetime objects in Python
In this article, we are going to see how to convert the “Unknown Format” string to the DateTime object in Python.
Suppose, there are two strings containing dates in an unknown format and that format we don’t know. Here all we know is that both strings contain valid date-time expressions. By using the dateutil module containing a date parser that can parse date strings in many formats.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
Let’s see some examples that illustrate this conversion in many ways:
Example 1: Convert “unknown format” strings to datetime objects
In the below example, the specified unknown format of date string “19750503T080120” is being parsed into a valid known format of the DateTime object.
Python3
# Python3 code to illustrate the conversion of# "unknown format" strings to DateTime objects # Importing parser from the dateutil.parserimport dateutil.parser as parser # Initializing an unknown format date stringdate_string = "19750503T080120" # Calling the parser to parse the above# specified unformatted date string# into a datetime objectsdate_time = parser.parse(date_string) # Printing the converted datetime objectprint(date_time) |
Output:
1975-05-03 08:01:20
Example 2: Convert current date and time to datetime objects
In the below example, the current date and time have been parsed into datetime object.
Python3
# Python3 code to illustrate the conversion of# "unknown format" strings to DateTime objects # Importing parser from the dateutil.parser and# dt from datetime moduleimport dateutil.parser as parserimport datetime as dt # Calling the now() function to# return the current datetimenow = dt.datetime.now() # Calling the isoformat() to return the# current datetime in isoformat and print itprint(now.isoformat()) # Now calling the parser to parse the# above returned isoformat datetime into# a valid known format of datetime objectprint(parser.parse(now.isoformat())) |
Output:
2021-08-20T13:35:23.829488 2021-08-20 13:35:23.829488
Sometimes the DateTime strings can be ambiguous like 01-02-2003 could mean January 2 or February 1. To remove this ambiguity we have two parameters like dayfirst and yearfirst that are illustrated in the below examples.
Example 3: Convert specific unknown format to datetime objects
In the below example, the specified datetime is “10-09-2021”. The parse() has used the parameter dayfirst as True and returns the output as “2021-09-10 00:00:00” i.e 9th October of the 2021 year.
Python3
# Python3 code to illustrate the conversion of# "unknown format" strings to DateTime objects # Importing parser from the dateutil.parserimport dateutil.parser as parser # Now parsing the "10-09-2021" datetime with# dayfirst parameterB = parser.parse("10-09-2021", dayfirst = True) # Printing the parsed datetimeprint(B) |
Output:
2021-09-10 00:00:00

