I use the instruction as shown below to create a datetime object from a string:
t = datetime.strptime("0023-10-10", "%Y-%m-%d")
Later, somewhere in my code, the t object is used and the strftime method is called with the same format string:
t.strftime("%Y-%m-%d")
This results in a ValueError: year=23 is before 1900; the datetime strftime() methods require year >= 1900 ValueError: year=23 is before 1900; the datetime strftime() methods require year >= 1900 .
It seems that the validation of input% Y differs in two similar methods. Therefore, I must do the following to make sure that I do not accept some bad years, for example 23 :
try: format = "%Y-%m-%d" t = datetime.strptime("0023-10-10", format) t.strftime(format) except ValueError: ...
I wonder if there is a better way to do this check.
source share