Validate SQL formatted date

https://stackoverflow.com/questions/16870663/how-do-i-validate-a-date-string-format-in-python



def is_sql_date(str_val):
    date_text = str_val.strip()
    try:
        datetime.datetime.strptime(date_text, '%Y-%m-%d')
        return date_text
    #except ValueError:
    #    raise ValueError("Incorrect data format, should be YYYY-MM-DD")
    except:
        return None



### test date validation
test_date = is_sql_date('')
test_date2 = is_sql_date('2017')
test_date3 = is_sql_date('2017-07-24')



Comments

Popular posts from this blog

Dive into the World of Fractional Shares: Your Path to Diversified Investments

High Gas Prices: How They Impact You and the Economy

Unlocking the Door to Your Dream Home: Strategies for First-Time Buyers