Python – How do you format a date string if you don’t know the format of date input?

How do you format a date string if you don’t know the format of date input?… here is a solution to the problem.

How do you format a date string if you don’t know the format of date input?

I have to read through a lot of files based on the file name. They can differ from the Y-M-D, YMD, M_D_Y, or Y_M_D formats. There may be others, but at the moment I can only use these.

I

need to be able to extract dates, I’ve done it with regular expressions, and formatted them in YMD format. For example, if my input string is 06_12_2018, I need to be able to format it as a 20180612 to compare with another file later.

What I’ve tried so far :

def cleanDate(date):
    datePatterns = [“%Y%m%d”, “%Y_%m_%d”, “%Y-%m-%d”, “%m_%d_%Y”]
    for pattern in datePatterns:
        if date in datePatterns:
            return datetime.strftime(date, “%Y%m%d”)
        else:
            print “String format not found!”
            return

Now that I’m watching, there’s no point in doing if date in datePatterns. What is the best way to solve this problem?

Solution

The best way to do this is to use try/except:

for pattern in datePatterns:
    try:
        return datetime.strptime(date, pattern)
    except ValueError:
        pass
else:
    # none of the datePatterns worked
    raise Exception('failed to parse')

Note that strptime is required here, not strftime. Wetware tip: p is used for p parsing, f is used for f formatting

They can vary from the format Y-M-D, YMD, M_D_Y, or Y_M_D. There could be others but at the moment this is all I am given to work with.

If other formats are available, consider using >dateutil.parser Instead, it uses heuristics to guess the format. It is quite popular, proven and reliable.

>>> from dateutil.parser import parse  # pip install python-dateutil
>>> parse("2018-05-12")
datetime.datetime(2018, 5, 12, 0, 0)

Related Problems and Solutions