Reputation:
In Python there are multiple DateTime parser's which can parse a date string automatically without proving the datetime format. e.g. DateParser, DateUtils. However, There is no option in any of them where the format in which the date was parsed is returned. What should be done to get the format with which automatic parser's parsed the date as in the example below? e.g parse("2019/05/20") -- > [datetime(2019,5,20,0,0) , "%Y/%m/%d"]
Upvotes: 3
Views: 149
Reputation: 3847
If you want the format string to be able to format datetime objects the same way, I don’t think this is possible, because these parsers (at least dateparser) can parse dates that simply cannot be expressed as a format string.
There is a feature request for it, though. Maybe it can be implemented in some way, or for a subset of all possible inputs.
If you want this for debugging purposes, to understand how a given string could cause a given datetime, I don’t think there is an easy way at the moment either. But if you are getting unexpected results, there are settings you can use to influence different parsing aspects to get the desired results. Date order, input format strings and locales can make quite a difference.
Upvotes: 1
Reputation: 301
If you look into the source code of those libraries, you can find out how they are parsing their dates.
For example, DataParser
is using a combination of regex and the datetime string formatting directives. Here is the link to their parser: https://github.com/scrapinghub/dateparser/blob/master/dateparser/parser.py
Upvotes: 0