datetime format is quite a pain, I keep having to deal with different formats.
I am validating the format to contain date and time in a specific format like this:
def validate_datetime(value):
try:
checktype = datetime.strptime(value, '%Y-%m-%dT%H:%M')
if not isinstance(checktype, datetime):
return False
except Exception as e:
return False
return True
now if this value also contains seconds, such as '2020-01-01T10:00:00', the method will return False. I could nest multiple try: -blocks within each other and only return False if ALL the checks fail, but I feel like there must be a better way.
How do you check if a string is a date / datetime / with / without seconds, and not having maximum overhead and checking for every case possible?