Mostly Linux & Python syntax notes and hyperlinks.

Tuesday, March 30, 2010

python: converting from written out date to numeric

Use the chart at the bottom of http://docs.python.org/library/datetime.html for the %B, %d, %m etc.
>>> import time
>>> f=time.strptime("November 29, 2010", "%B %d, %Y")
>>> f[0]
2010
>>> f[1]
11
>>> f[2]
29
>>> g=time.strftime("%m/%d/%y",f)
>>> g
'11/29/10'
>>> h=time.strftime("%m/%d/%y",time.strptime("October 4, 2012", "%B %d, %Y"))
>>> h
'10/04/12'
>>> time.strftime("%m/%d/%y",time.strptime("May 3, 2011", "%B %d, %Y"))
'05/03/11'
>>>time.strftime("%B %d, %Y",time.strptime("23-Jun-14","%d-%b-%y"))
'June 23, 2014'

No comments:

Post a Comment

Blog Archive