2

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"]

Quamber Ali
  • 2,170
  • 25
  • 46
  • What is the question? – Bayleef Aug 31 '19 at 23:58
  • How can i get the format with which the date was parsed? –  Aug 31 '19 at 23:59
  • I had the same problem. Eventually I created a list of common datetime formats and parsed string with them in _Try-Catch_ to get the right format. I don't know the use case but you can always ask user to provide the format (of course with with proper help and example) – Syed Mohammad Hosseini Dec 14 '19 at 10:25

2 Answers2

1

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.

Gallaecio
  • 3,620
  • 2
  • 25
  • 64
0

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

awakenedhaki
  • 186
  • 5
  • DateParser has 5 modules internally.. i tried to do that. Its very complicated –  Sep 01 '19 at 00:34
  • Well, it takes practice to look at these projects. I simply saw a `parser.py`, and assumed that that is probably where they are parsing their dates... Also, there is a search module, that is also a potential place where they are searching for dates. – awakenedhaki Sep 01 '19 at 00:38