Ad

How To Format Multiple Date Formats Into Single Date In Python

I have a list in Python with different date formats:

list1 = ["30-4-1994", "1994-30-04", "30/04/1994",
         "30-apr-1994", "30/apr/1994","1994-30-apr"]

I want to format multiple date formats into a single date like dd-mm-yyyy

How can I do that?

Ad

Answer

In an ideal world, you know the format of your inputs.

Where this is not possible, I recommend you use a 3rd party library for mixed format dates.

Two libraries that come to mind are dateutil (via dateutil.parser.parse) and pandas (via pandas.to_datetime). Below is an example implementation with the former.

Note the only occasion when parser.parse was unsuccessful had to be covered with a manual conversion via datetime.strptime. datetime is part of the standard Python library.

from datetime import datetime
from dateutil import parser

list1 = ["30-4-1994", "1994-30-04", "30/04/1994",
         "30-apr-1994", "30/apr/1994","1994-30-apr"]

def converter(lst):
    for i in lst:
        try:
            yield parser.parse(i)
        except ValueError:
            try:
                yield parser.parse(i, dayfirst=True)
            except ValueError:
                try:
                    yield datetime.strptime(i, '%Y-%d-%b')
                except:
                    yield i

res = list(converter(list1))

# [datetime.datetime(1994, 4, 30, 0, 0),
#  datetime.datetime(1994, 4, 30, 0, 0),
#  datetime.datetime(1994, 4, 30, 0, 0),
#  datetime.datetime(1994, 4, 30, 0, 0),
#  datetime.datetime(1994, 4, 30, 0, 0),
#  datetime.datetime(1994, 4, 30, 0, 0)]

You can then format into strings any way you like using datetime.strptime:

res_str = [i.strftime('%d-%m-%Y') for i in res]

# ['30-04-1994',
#  '30-04-1994',
#  '30-04-1994',
#  '30-04-1994',
#  '30-04-1994',
#  '30-04-1994']
Ad
source: stackoverflow.com
Ad