I am trying to strip off the time stamp from a string for e.g. '9/10/20151:03 PM' Note that there is intentionally no space between the date and time.
Here's the code:
datetime.strptime(date, "%m/%d/%Y%I:%M %p") However, I might get bad strings like '9/10/2015 1:03 PM' ( with a space between date and time). I am contemplating to grep the incoming string for a pattern and decide which pattern to use in my strptime method.
My question is that is there a better way to do this? I looked at documentation of strptime method and it doesn't support multiple patterns. The only other way I can think of is following ( but it doesn't support more than two patterns )
try: time = datetime.strptime(date, "%m/%d/%Y%I:%M %p") except Exception as e: time = datetime.strptime(date, "%m/%d/%Y %I:%M %p") CONSTRAINTS: 1. Cannot depend on a library except datetime since I am going to push it to production as a bug fix.