I realize there's a problem there in determining what, for example, "year 40" is. I'm happy with setting a bounds, so that any year under 50 should be interpreted as 20xx and any year over 50 as 19xx. This hacky little exercise of mine doesn't need to be Y2.05K compatible! :)8/17/80 ---> 8/17/1980 10/19/82 ---> 10/19/1982 10/9/01 ---> 10/9/2001
x if c else yform:
def convert(date):
m, d, y = date.split('/')
return "/".join([m, d, '%d%s' % (19 if int(y) >= 50 else 20, y)])
I don't think there's any straightforward way to do it without splitting the string on a separate line.def convert(d): return d[:-2]+str(19+(int(d[-2:])<5>5>
import datetime
def reformat_date(date_string):
return datetime.datetime.strptime(date_string, '%m/%d/%y').strftime('%m/%d/%Y')
My suggestion, though, would be to parse the data when you read it into a datetime object (leaving off the strftime call here) and only change it back into a string when you output it. That way, you'll be able to sort, compare, etc. with your parsed dates and get proper results. You may find (say, if you're inserting into a database) that you can just use the datetime object and not worry about formatting it at all.# import the datetime class from the datetime module
from datetime import datetime
def reformat_date(date_string):
timepoint = datetime.strptime(date_string, "%m/%d/%y")
return timepoint.strftime("%m/%d/%Y")I think it's easier to see what is going on this way, too.from datetime import datetime
def reformat_date(date_string):
timepoint = datetime.strptime(date_string, "%m/%d/%y")
day = timepoint.date()
return day.isoformat()You are not logged in, either login or create an account to post comments
if tokens[2] > 50:
print("19"+str((tokens[2]))
else:
print("20"+str((tokens[2]))
I think you should be able to piece it together from there. There are shorter options, you don't really need to use split since you know those digits are the last two of the string you can use string[-2:] to get the last two characters.... However you get them just case to an int compare and create a new string.
posted by magikker at 2:23 AM on March 4, 2009