I have dates in a form Fri 27th Augthat is a nightmare programmatically, as I am sure you can imagine.
I am wondering what is the best way to convert them to a date form in the USA 08/27/13. I need to specify the year from the month, i.e. Aug-Dec implies 13, and Jan-Jul implies 14.
I was thinking about how to do this in regex, or even just do a series of line replacements.
But the complication is that I have a list of strings, not all of which are dates of this form. If others have numbers inside, how can I check the date of this form and then replace if there is one?
eg.
list = ['not a date', 'als0 not a dat3', 'Wed 5th Jan', ... , 'no date here']
The test requirement makes the regular expression suitable, but I read a lot about SO versus using rein Python, although I don't know why. Should I (learn to use enough, and) use it?
With @Allan's answer, I was able to solve my problem with
def is_date(string):
tmp = string.replace('th','')
string = tmp.replace('rd','')
tmp = string.replace('nd','')
string = tmp.replace('st','')
try:
d = strptime(string, "%a %d %b")
date = str(d[1]) + "/" + str(d[2]) + "/"
if d[1] >= 8:
date += "13"
else:
date += "14"
return date
except ValueError:
return 0
Thanks for your answers, @Allan, @adsmith and @codnodder.