2

I am using dateutil.parser to extract date from text, as follows:

def extract_date(text):
    try:
        date = dparser.parse(text, fuzzy=True, dayfirst=True)
        return f'{date.day}/{date.month}/{date.year}'

    except Exception:
        return 'NOTFOUND'

print(extract_date('some sample text 01-12-24))

>>> 1/12/2024

Which works perfectly fine when the date is fully defined. But when I give text like this which is incomplete,

print(extract_date('some sample text 1-12'))
>>> 1/12/2022

As you can see here 'year' is not defined but it automatically adds the current year which is '2022' which I don't want!

I tried making fuzzy=False but then it is not able to extract the date at all. So, is there any solution to avoid this behavior or any kind of technique?

  • As you can see from the documentation `https://dateutil.readthedocs.io/en/stable/parser.html` returns a `datetime.datetime` object, which always contains a year - so what you're asking for is not possible with this function – oskros Feb 22 '22 at 12:34
  • also, maybe this helps : https://stackoverflow.com/questions/13429524/python-smart-date-parsing-without-a-year-specified – oskros Feb 22 '22 at 12:37
  • `dateparser` package includes options to handle incomplete dates: https://dateparser.readthedocs.io/en/latest/settings.html#handling-incomplete-dates – Alan Buxton Feb 23 '23 at 14:41

0 Answers0