Convert "unknown format" strings to datetime objects in Python - GeeksforGeeks

31 Aug, 2021
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.
Let’s see some examples that illustrate this conversion in many ways:
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
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.
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
Picked
Python datetime-program
Python
Python Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
How To Convert Python Dictionary To JSON?
How to drop one or multiple columns in Pandas Dataframe
Check if element exists in list in Python
Selecting rows in pandas DataFrame based on conditions
Defaultdict in Python
Python | Split string into list of characters
Python | Get dictionary keys as a list
Python | Convert a list to dictionary
Python program to check whether a number is Prime or not