简体   繁体   English

如何仅打印日期而不包括datetime.datetime.strptime()中的时间

[英]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.

相关问题 python中添加了两个datetime.datetime.strptime()。time()对象 - Addition of two datetime.datetime.strptime().time() objects in python datetime.datetime.strptime 表示格式错误。 怎么解决? - datetime.datetime.strptime says wrong format. how to solve? date 2.4中没有datetime.datetime.strptime - datetime.datetime.strptime not present in Python 2.4.1 *NIX 上的 datetime.datetime.strptime 问题 - Issue with datetime.datetime.strptime on *NIX 解析datetime.datetime.strptime的麻烦 - trouble with parsing datetime.datetime.strptime 错误处理datetime.datetime.strptime - error handling datetime.datetime.strptime 使用 Matplotlib 绘制时间序列:使用 datetime.datetime() 有效,但 datetime.datetime.strptime(str, format) 无效 - Plotting Time Series with Matplotlib: Using datetime.datetime() works but datetime.datetime.strptime(str, format) does not datetime.datetime.strptime 将字符串转换为 datetime 它发生错误未转换的日期还押:2 - datetime.datetime.strptime to convert a string to datetime it occurs the mistakes unconverted date remands: 2 使用可变的输入长度的datetime.datetime.strptime() - datetime.datetime.strptime() using variable input length 在具有BST时区的Windows上使用python datetime.datetime.strptime - Using python datetime.datetime.strptime on windows with BST timezone
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM