I have MMDDYY
dates, i.e. today is 111609
How do I convert this to 11/16/2009
, in Python?
I have MMDDYY
dates, i.e. today is 111609
How do I convert this to 11/16/2009
, in Python?
I suggest the following:
import datetime
time = datetime.datetime.strptime("111609", "%m%d%y")
print time.strftime("%m/%d/%Y")
This will convert 010199 to 01/01/1999 and 010109 to 01/01/2009.
date = '111609'
new_date = date[0:2] + '/' + date[2:4] + '/' + date[4:6]
>>> s = '111609'
>>> d = datetime.date(int('20' + s[4:6]), int(s[0:2]), int(s[2:4]))
>>> # or, alternatively (and better!), as initially shown here, by Tim
>>> # d = datetime.datetime.strptime(s, "%m%d%y")
>>> d.strftime('%m/%d/%Y')
'11/16/2009'
one of the reasons why the strptime() approach is better is that it deals with dates close to contemporary times, with a window, i.e. properly handling dates in the latter part of the 20th century and dates in the early part of the 21st century properly.