parse_date

pyrcs.parser.parse_date(str_date, as_date_type=False)[source]

Parses a string representation of a date into a formatted date.

This function attempts to parse a string date (even with slight errors or non-standard formats) into either a string in the “YYYY-MM-DD” format or a datetime.date object.

Parameters:
  • str_date (str) – The date as a string, whose format can vary and may include month names or other elements.

  • as_date_type (bool) – If True, returns the result as a datetime.date object; if False (default), returns the result as a formatted string.

Returns:

The parsed date either as a string in “YYYY-MM-DD” format or as a date object.

Return type:

str | datetime.date

Examples:

>>> from pyrcs.parser import parse_date
>>> str_date_dat = '2020-01-01'
>>> parse_date(str_date_dat)
'2020-01-01'
>>> str_date_dat = '2020-jan-01'
>>> parse_date(str_date_dat)
'2020-01-01'
>>> parse_date(str_date_dat, as_date_type=True)
datetime.date(2020, 1, 1)