You can convert them to strings and just lay them out:
import datetime d = datetime.datetime(2012, 5, 25) m = str(d.month).rjust(2, '0') print(m)
Or you can just str.format
:
import datetime d = datetime.datetime(2012, 5, 25) print("{:0>2}".format(d.month))
EDIT: To answer the updated question, have you tried to do this?
import datetime d = datetime.datetime(2012, 5, 25) print("{:0>4}-{:0>2}-{:0>2}".format(d.year, d.month, d.day))
You said you originally printed them using line formatting, so what did you change? This code:
print "%s-%s-%s"%(date.year, date.month, date.day, etc., len(str) )
It makes no sense, as I'm a little unclear as to which arguments you pass. I assume it is just date.year
, date.month
and date.day
, but this is unclear. What action do you perform with len(str)
?
source share