How to print only date and not include time in datetime.datetime.strptime ()

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?

+5
source share
4 answers
>>> t.strftime('%Y-%m-%d')
'2011-12-02'
+11
source

C strftime():

print t.strftime('%Y-%m-%d')
+2
source

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)
0
source

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

0
source

All Articles