I would like to parse a date that can come in several formats, that I know beforehand. If I could not parse, I return nil. In ruby, I do like this:
DATE_FORM
After your tip, RocketDonkey, and the one from Bakuriu, I could write a shorter version. Any problem with it?
def parse_or_none(date): for date_format in DATE_FORMATS: try: return datetime.strptime(date, date_format) except ValueError: pass return None