feners
feners

Reputation: 675

Problems with turning a timedelta calculation into " %A, %B %d, %Y"

I want to use the following code snippet to turn a generator of upcoming days into readable words, that I will store in a list, using .strftime(" %A, %B %d, %Y").

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

However, what gets returned is in an unreadable format. The methods I know of turning that format into a readable format is not working.

Upvotes: 3

Views: 104

Answers (1)

Padraic Cunningham
Padraic Cunningham

Reputation: 180441

You code works fine if you add parens then call 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)

Output:

[' 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']

Upvotes: 4

Related Questions