diff --git a/common/helper_excel.py b/common/helper_excel.py index 48baeaf..4abc9c0 100644 --- a/common/helper_excel.py +++ b/common/helper_excel.py @@ -302,16 +302,31 @@ def to_locale_str(a): return "{:,}".format(int(a)) -def convert_str_to_date(string): +def convert_str_to_date(string,with_datetime=None): string = str(string).strip() - try: - return datetime.strptime(string, '%Y-%m-%dT%H:%M:%S.%fZ').date() - except ValueError: - try: - return datetime.strptime(string, '%Y-%m-%dT%H:%M:%SZ').date() # Added format without milliseconds - except ValueError: - try: - return datetime.strptime(string, '%Y-%m-%d').date() - except ValueError: - return None + + date_formats = [ + '%Y-%m-%dT%H:%M:%S.%fZ', + '%Y-%m-%dT%H:%M:%SZ', + '%Y-%m-%dT%H:%M:%S.%f%z', + '%Y-%m-%dT%H:%M:%S%z', + '%Y-%m-%dT%H:%M:%S.%f', + '%Y-%m-%dT%H:%M:%S', + '%Y-%m-%d %H:%M:%S.%f', + '%Y-%m-%d %H:%M:%S', + '%Y-%m-%dT%H:%M:%S.%f%z', + '%Y-%m-%d' + ] + + for fmt in date_formats: + try: + if with_datetime: + date = datetime.strptime(string, fmt) + else: + date = datetime.strptime(string, fmt).date() + return date + except ValueError: + continue + + return None