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?

4
  • 2
    possible duplicate of Convert Date String to DateTime Object in Python Commented May 18, 2012 at 18:17
  • 6
    You've asked over 150 python questions here... surely this is something that could be googled, right? Commented May 18, 2012 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. Commented May 18, 2012 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. Commented May 18, 2012 at 18:48

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.