Skip to content

Instantly share code, notes, and snippets.

@acviana
Created October 2, 2012 00:51
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 acviana/3815523 to your computer and use it in GitHub Desktop.
Save acviana/3815523 to your computer and use it in GitHub Desktop.
This is a function to check if a date string is in 'MMM DD YYYY' format, e.g. 'Oct 1 2012'.
def check_date_format(date):
'''
Check if that date string has a 'MMM DD YYYY' format. Return a boolean.
'''
output = True
if isinstance(date,unicode) == False:
return False
month, day, year = date.split()
if set(month) >= set(string.ascii_letters):
return False
if len(month) != 3:
return False
if set(day) >= set(string.digits):
return False
if len(day) not in [1,2]:
return False
if set(year) >= set(string.digits):
return False
if len(year) != 4:
return False
return output
@embray
Copy link

embray commented Oct 2, 2012

First of all, it's not necessary to write isinstance(...) == False. For anything that returns a boolean result just use not, such as if not isinstance(...).
Second of all, datetime.strptime() :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment