I am using Python 3.11.
I have a list of dates and datetimes in the csv file.
I want to read it and add one more column with the datetime format of the date column
one workaround is to have a large list of possible date and datetime formats and parse using datetime.strptime
. The Challenge here is to keep and manage a large dataset of date-time formats. It can have any separator in between and there are a lot of permutaions and combinations.
Is there any package does that?
I can use dateutils but it doesn’t give the format. It directly converts it to datetime type
Here is what I did
def check_date_format(date_string):
common_formats = [
"%Y-%m-%d", "%d-%m-%Y", "%m/%d/%Y", "%m-%d-%Y", "%Y/%m/%d",
"%d %B %Y", "%B %d, %Y", "%d %b %Y", "%b %d, %Y", "%Y%m%d",
"%d.%m.%Y", "%d %B %Y", "%B %d %Y", "%Y %b %d", "%m.%d.%Y"
]
for date_format in common_formats:
try:
parsed_date = datetime.strptime(date_string, date_format)
return date_format
except ValueError:
continue
date_string = "25-12-2021"
matched_format = check_date_format(date_string)