Last active
June 19, 2017 18:43
-
-
Save devMlGUE/1934c4fd701ec07c2827cbecdddac4d4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
from datetime import datetime, date | |
def is_date(date, valid_formats): | |
''' Devuelve un objeto "datetime" si la cadena ingresada tiene alguno de | |
los patrones de fecha en "valid_formats". ''' | |
for fmt in valid_formats: | |
try: | |
return datetime.strptime(date, fmt) | |
except ValueError: | |
pass | |
return False | |
valid_formats = ( | |
'%Y-%m-%d', | |
'%d-%m-%Y', | |
'%Y\%m\%d', | |
'%Y %m %d' | |
) | |
print(is_date('22/04/1992', valid_formats)) | |
print(is_date('22 04 1992', valid_formats)) | |
print(is_date('22-04-1992', valid_formats)) | |
print(is_date('22\04\1992', valid_formats)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment