Search code examples
pythondateparser

In Python dateparser, how to parse a date string with year as optional without default value


I am using dateparser.parse() function to parse a lot of date strings. Some of them have year some not. Currently, I have to call the function twice to know if a string has year as following:

parsed_date = dateparser.parse(date_str, settings={"REQUIRE_PARTS": ["day", "month", "year"]})

if not parsed_date:
    parsed_date = dateparser.parse(date_str, settings={"REQUIRE_PARTS": ["day", "month"]})

Since dateparser.parse() function is expensive, it takes long time to process these date strings. I want to know if it possible to combine them into one function call so that it returns either day and month (without year) or day, month and year respectively.

Currently, if I call dateparser.parse(date_str, settings={"REQUIRE_PARTS": ["day", "month", "year"]}) for strings without years, it returns current year as default, which is not what I want.


Solution

  • You could use a relative base to control what year gets imputed when no year is in the date. In this example, we're having 9999 be the default year, and you could check for this in your loop and route accordingly.

    import dateparser
    import datetime
    date_str = '01-01-2020'
    date_str_no_year = '01-01'
    dateparser.parse(date_str, settings={'REQUIRE_PARTS':['day','month'], "RELATIVE_BASE":datetime.datetime(9999,1,1)})
    

    datetime.datetime(2020, 1, 1, 0, 0)

    dateparser.parse(date_str_no_year, settings={'REQUIRE_PARTS':['day','month'], "RELATIVE_BASE":datetime.datetime(9999,1,1)})
    

    datetime.datetime(9999, 1, 1, 0, 0)