Python get valid date format from date string

Granitosaurus :

Is there a way to guess datetime format of a given string in python?

e.g. desired usage would be:

> guess_format('2020-02-24 07:22')
'%Y-%m-%d %H:%M'

There's dateutil project which automates datetime string conversion to valid Datetime objects:

> from dateutil.parser import parse
> parse('2020-02-24 07:22')
datetime.datetime(2020, 2, 24, 7, 22)

but can it produce valid formatting strings?

luigigi :

The pydateinfer package provides the possibility to infer the datetime format string of a given date string.

Example:

>>> import dateinfer
>>> dateinfer.infer(['Mon Jan 13 09:52:52 MST 2014', 'Tue Jan 21 15:30:00 EST 2014'])
'%a %b %d %H:%M:%S %Z %Y'

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=3813&siteId=1