David542
David542

Reputation: 110482

Convert date string to datetime object

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?

Upvotes: 1

Views: 339

Answers (1)

Burhan Khalid
Burhan Khalid

Reputation: 174718

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

Upvotes: 2

Related Questions