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

Jack
  • 102
  • 1
  • 14
Vivek Srinivasan
  • 2,687
  • 3
  • 17
  • 17
  • 2
    How should such a library decide if the `11` in `2016-11-04` is the month or the day of month? –  Nov 04 '16 at 07: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. – Dr. V Nov 04 '16 at 08:34
  • what's your purpose? As Lutz said, it's a one-to-multi question. – Kir Chou Nov 04 '16 at 08: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... – Vivek Srinivasan Nov 04 '16 at 09:44
  • Not only is this arguably off-topic, [it has been asked before](http://stackoverflow.com/questions/9507648/datetime-from-string-in-python-best-guessing-string-format), in [various](http://stackoverflow.com/questions/26238159/guessing-date-format-for-many-identically-formatted-dates-in-python) [forms](http://stackoverflow.com/questions/34073502/python-getting-the-date-format). – John Y Nov 04 '16 at 13:24
  • Y down votes...although some useful discussion happened through it .. – Vivek Srinivasan Nov 04 '16 at 17:42

1 Answers1

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.

zochamx
  • 850
  • 8
  • 17