user1781482 user1781482 - 3 months ago 10
Python Question

Convert multiple types of dates

I'm working with data that comes from different places and need to convert dates into the same format. Below are few examples of what I have:

Thu Dec 03 07:27:23 GMT 2015
3-Dec-15
2015-12-04T06:58:54Z
23-Sep-2015 07:03:37 UTC


The desired output format should be the same for all dates, like this:

12/03/2007
12/03/2015
12/04/2015
09/23/2015


Any suggestions how to achieve that with Python? Thanks in advance!

Answer

Yes, the dateutil library provides date format detection with the parse function :

from dateutil.parser import parse
parse(text).strftime("%m/%d/%Y")
Comments