删除日期时间功能的时间?

时间:2013-03-22 09:40:57

标签: python django templates datetime

我在此值中有变量{{value.time}}执行

March 22, 2013, 2:30 a.m

但我想在模板文件中删除此值的2:30 a.m(时间)。我怎么能这样做?

2 个答案:

答案 0 :(得分:9)

将日期作为扩展名

{{ value.time.date }}

答案 1 :(得分:3)

>>> from datetime import datetime,date
>>> date_str = 'March 22, 2013, 2:30 a.m'
>>> date_str = date_str.replace('a.m','AM')
>>> formatter_string = "%B %d, %Y, %I:%M %p" 
>>> datetime_object = datetime.strptime(date_str, formatter_string)
>>> date_object = datetime_object.date() # date object without '2:30 AM'
>>> date_object
datetime.date(2013, 3, 22)
>>> formatter_string = "%B %d, %Y" 
>>> datetime_string = datetime.strftime(date_object, formatter_string)
>>> datetime_string
'March 22, 2013'