feners feners - 1 year ago 94
Python Question

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.

Answer Source

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)]


[' 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']
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download