I have this code:
>>> import datetime >>> l = '2011-12-02' >>> t = datetime.datetime.strptime(l, '%Y-%m-%d') >>> print t 2011-12-02 00:00:00
My question is, is it possible to print only 2011-12-02?
2011-12-02
>>> t.strftime('%Y-%m-%d') '2011-12-02'
C strftime():
strftime()
print t.strftime('%Y-%m-%d')
I think you should use this as
d = datetime.datetime(2011,7,4) print '{:%Y-%m-%d}'.format(d)
or your code:
import datetime l = '2011-12-02' t = datetime.datetime.strptime(l, '%Y-%m-%d') print '{:%Y-%m-%d}'.format(t)
You must specify the output format for the date, for example. using
print t.strftime("%Y-%m-%d")
The whole letter after "%" represents the format:% d - day number,% m - month number,% y - last two digits of the year,% Y - all year