Home > Enterprise >  dateparser.parse is swapping day and month for values less than 13
dateparser.parse is swapping day and month for values less than 13

Time:10-16

So, I'm using dateparser.parse to extract year, month and day. For values more than 13, it works fine. However, for values less than 13, it is swapping day and month. For example, in DD/MM/YYYY format, if the input is 13/11/1990, the output is as expected Month -> 11 and Day -> 13. But if the input is 8/9/1990, the output is Month -> 8 and Day -> 9.

dateparser.parse('13/11/1990','d/M/yyyy')
datetime.datetime(1990, 11, 13, 0, 0)

dateparser.parse('8/9/1990','d/M/yyyy')
datetime.datetime(1990, 8, 9, 0, 0)

CodePudding user response:

The documentation says:

dateparser.parse(date_string, date_formats=None, ...)
  • date_formats (list) – A list of format strings using directives as given here. The parser applies formats one by one, taking into account the detected languages/locales.

Neither are you supplying the format string as list, nor does it adhere to the linked format. It should probably look like this:

dateparser.parse('8/9/1990', ['%d/%m/%Y'])
  • Related