diff --git a/dateparser/date.py b/dateparser/date.py index fc434a6f2..7901fcd99 100644 --- a/dateparser/date.py +++ b/dateparser/date.py @@ -438,6 +438,13 @@ def get_date_data(self, date_string, date_formats=None): period='day', locale='en') """ + + if date_formats: + try: + validate_date_format(date_string, date_formats) + except InvalidDateString as e: + pass + if not isinstance(date_string, str): raise TypeError('Input type must be str') @@ -521,3 +528,14 @@ def _get_locale_loader(cls): if not cls.locale_loader: cls.locale_loader = LocaleDataLoader() return cls.locale_loader + +def validate_date_format(date_string, date_formats): + for date_format in date_formats: + try: + datetime.strptime(date_string, date_format) + return + except ValueError: + raise InvalidDateString("Date string does not match any of the given formats") + +class InvalidDateString(Exception): + pass \ No newline at end of file