I have a string that I need to convert to datetime format. The problem is that it doesn't always have the same format. Right now I'm using try, except to try all the formats that I might encounter (see code below). Is there a way to do this generically for all time formats?
import datetime
def get_time(start_time):
try:
start_time = datetime.datetime.strptime(start_time, '%m/%d/%y %H:%M:%S')
return start_time
except:
try:
start_time = datetime.datetime.strptime(start_time, '%H:%M:%S')
return start_time
except:
try:
start_time = datetime.datetime.strptime(start_time, '%m/%d/%Y %H:%M')
return start_time
except:
print("Error could not convert datetime format")
forloop.