将timedelta计算转换为" %A,%B%d,%Y"

时间:2015-06-20 16:11:18

标签: python timedelta

我想使用以下代码片段将即将到来的日期生成器转换为可读单词,我将使用.strftime存储在列表中("%A,%B%d,%Y&#34 ;)

base = datetime.today()
date_list = [base - timedelta(days=x) for x in range(0, 7)]
datescroll_list = ()

然而,返回的内容是不可读的格式。我知道将该格式转换为可读格式的方法无效。

1 个答案:

答案 0 :(得分:4)

如果您添加parens然后调用strftime:

,您的代码可以正常工作
from datetime import datetime,timedelta
base = datetime.today()
date_list = [(base - timedelta(days=x)).strftime(" %A, %B %d, %Y") for x in range(0, 7)]
print(date_list)

输出:

[' Saturday, June 20, 2015', ' Friday, June 19, 2015', ' Thursday, June 18, 2015', ' Wednesday, June 17, 2015', ' Tuesday, June 16, 2015', ' Monday, June 15, 2015', ' Sunday, June 14, 2015']