Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I convert a string to a datetime without many nested try…excepts?

I’m trying to check user input of a date/time in several allowable formats. (I know about the dateutil library. It’s not what I’m looking for in this case.)

If some user input was accepted, the function must return a datetime object.

If ALL "try…except" fail — the function must return NONE. But I have 30-50 different date/time formats that I need to check.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I’m confused by the huge indentation in my code! How do I organize this format checking in a good style with GOOD performance?


# Test format check program
import datetime

def datetime_format_check(str):
    try:
        dt = datetime.datetime.strptime(str, "%y-%m-%d %H:%M")
        return dt
    except:
        try:
            dt = datetime.datetime.strptime(str, "%Y-%m-%d %H:%M")
            return dt
        except:
            try:
                dt = datetime.datetime.strptime(str, "%y-%m-%d")
                return dt
            except:
                try:
                    dt = datetime.datetime.strptime(str, "%Y-%m-%d")
                    return dt
                except:
                    try:
                        dt = datetime.datetime.strptime(str, "%H:%M")
                        return dt
                    except:
                        try:
                               # . . .
                               # many many try...except blocks )))
                               # . . .
                                   return None  # last except far far away from a screen border. ))))

while True:
    str = input("Input date: ")
    print("Result: ", datetime_format_check(str))

>Solution :

Repetitive code? Well, that just begs to be replaced with a loop.

Put all of the formats in a list and iterate over it, checking each format:

def datetime_format_check(s):
    formats = ["%y-%m-%d %H:%M", "%Y-%m-%d %H:%M", "%y-%m-%d"] # etc
    for format in formats:
        try:
            dt = datetime.datetime.strptime(s, format)
            return dt
        except ValueError:
            pass
    return None

Some minor corrections I made to your code:

  • Don’t name your argument str; it shadows the builtin.
  • Don’t use a bare except:, always catch the specific exception.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading