I have a hacky solution but it feels completely wrong. I'm simply doing a compare of dates (one starts as a string which I convert) and my start_date
sometimes contains milliseconds. I was running this:
from datetime import datetime
def main():
start_date = "2019-05-22 20:00:02.1231"
try:
start_date = datetime.strptime(start_date, '%Y-%m-%d %H:%M:%S.%f')
print("i have milliseconds")
except:
start_date = datetime.strptime(start_date, '%Y-%m-%d %H:%M:%S')
print("no milliseconds")
if start_date < datetime.now():
print("yipee")
if __name__ == "__main__":
main()
There is also a chance that there are no seconds, so I could have:
start_date = "2019-05-22 20:00"
I could add another try
but I think I'm just missing a simple solution. I found this: python strptime format with optional bits which suggests use try
so I would use try
twice in this case, so is that the answer?