[英]how to print only date and not include the 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, it is possible that only the 2011-12-02
will be printed? 我的问题是,有可能只会打印
2011-12-02
吗?
>>> t.strftime('%Y-%m-%d')
'2011-12-02'
使用strftime()
:
print t.strftime('%Y-%m-%d')
I think you should use like this 我想你应该这样用
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 have to specify the output format for the date, eg by using 您必须指定日期的输出格式,例如使用
print t.strftime("%Y-%m-%d")
All the letter after a "%" represent a format: %d is the day number, %m is the month number, %y is the year last two digits, %Y is the all year “%”后面的所有字母代表格式:%d是日期编号,%m是月份编号,%y是最后两位数字,%Y是全年
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.