Skip to content

Instantly share code, notes, and snippets.

@devMlGUE
Last active June 19, 2017 18:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devMlGUE/1934c4fd701ec07c2827cbecdddac4d4 to your computer and use it in GitHub Desktop.
Save devMlGUE/1934c4fd701ec07c2827cbecdddac4d4 to your computer and use it in GitHub Desktop.
#!/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