str_to_list_of_dates

str_to_list_of_dates#

str_to_list_of_dates(date_str: list | str, date_format: str = '%Y-%m-%dT%H:%M:%S', additional_separator: str = '') list[source]#

Convert a string containing a list of dates to a list of datetime.datetime.

Also accepted date formats:

  • now: datetime.today()

  • Usual JSON date format: %Y-%m-%d

  • Already formatted datetimes and dates

Parameters:
  • date_str (Union[list, str]) – Date as a string

  • date_format (str) – Format of the date (as ingested by strptime)

  • additional_separator (str) – Additional separator

Returns:

A list containing datetimes objects

Return type:

list

Example

>>> # Default date format (isoformat)
>>> str_to_list_of_dates("20200909105055, 2019-08-06;19560702121212     2020-08-09",
>>>                      date_format="%Y%m%d%H%M%S",
>>>                      additional_separator=" ")
[datetime(2020, 9, 9, 10, 50, 55), datetime(2019, 8, 6), datetime(1956, 7, 2, 12, 12, 12), datetime(2020, 8, 9)]