Created
October 2, 2012 00:51
-
-
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'.
This file contains 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
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
First of all, it's not necessary to write
isinstance(...) == False
. For anything that returns a boolean result just usenot
, such asif not isinstance(...)
.Second of all,
datetime.strptime()
:)