2

We have python library datetime library to convert string date to date type of specified format using the following method:

datetime.strptime("2016-12-21", "%Y-%m-%d")

But do we have any library,given a date string suggest the format of the date. for the example given:

2016-12-21

as input, gives the output:

%Y-%m-%d

6
  • 2
    How should such a library decide if the 11 in 2016-11-04 is the month or the day of month? Commented Nov 4, 2016 at 7:54
  • Agree with Lutz, but have often same issue, then typically O(1000) such string dates available. So I loop brute-force and try to match a set of given formats with strptime and hopefully get a unique match. Commented Nov 4, 2016 at 8:34
  • what's your purpose? As Lutz said, it's a one-to-multi question. Commented Nov 4, 2016 at 8:40
  • i know its difficult to come up format like that....most of the times...when we extract web data....we come across varied date format...what we get is usually string date with different format...if we have any mechanism to detect the date format..it would be really nice to generalize.... pardon me if its too naive to ask... Commented Nov 4, 2016 at 9:44
  • Not only is this arguably off-topic, it has been asked before, in various forms. Commented Nov 4, 2016 at 13:24

1 Answer 1

3

I do not know a library with the functionality you ask but it isn't that difficult to write a function which will return the matching formats on given string.

Although this does not overcome the problem of a string being matched in more than one format.

Here is a sample for some static common formats on numeric based date.

date_formats = ["%Y-%m-%d", "%d-%m-%Y", "%m-%d-%Y", "%Y/%m/%d", "%d/%m/%Y", "%m/%d/%Y", "%Y.%m.%d", "%d.%m.%Y", "%m.%d.%Y", "%c", "%x"]

def string_to_date(str):
    match = []
    for fmt in date_formats:
        try:
            datetime.strptime(str, fmt)
        except ValueError as e:
            print(e)
            continue
        match.append(fmt)
    return match

And some representative output:

print(string_to_date("11.12.2015"))  # ['%d.%m.%Y', '%m.%d.%Y']
print(string_to_date("21/12/2015"))  # ['%d/%m/%Y']

Of course you can write code in order to build dynamically your formats but in that case i think, it's getting too broad.

In case you would go for a dynamic approach i would suggest to catch the possible delimiter first in your script and then try to build the format.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.