1

Take the following date inputs:

01/01/2012, 1-1-2012, 2012-01-20, 01012012, 20120101, 1.1.2012, 01.01.2012

How would I create one function that converts any typical date string into a datetime.date object. I would imagine you'd first strip any non-numeric character and then do some regex identification.

Let us make the assumption that if year comes first, then month comes second, and if year comes last (third), then month comes first. What would be the best function here?

David542
  • 104,438
  • 178
  • 489
  • 842
  • 2
    possible duplicate of [Convert Date String to DateTime Object in Python](http://stackoverflow.com/questions/2721782/convert-date-string-to-datetime-object-in-python) – Daenyth May 18 '12 at 18:17
  • 6
    You've asked over 150 python questions here... surely this is something that could be googled, right? – Mike Pennington May 18 '12 at 18:17
  • 1
    @Daenyth: perhaps the title of the two questions are similar, but the two questions are different. I do not believe any of the answers to that question would be valid for this. – David542 May 18 '12 at 18:38
  • @David542: Burhan's answer posted to this question would also answer the other question, which is a good indication that this covers no new ground. – Daenyth May 18 '12 at 18:48

1 Answers1

2

Use strptime along with this formatting table, split and a few try/catch blocks.

Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284