user11005819
user11005819

Reputation:

Using datetime.strftime and soutract it with timedelta

I am trying to use datetime.now() with timedelta and soustract it so I get the time for yesterday.

yesterday = datetime.now().strftime("%Y-%m-%d") -  timedelta(days=1)

But when I tried to do it, it gives me this error :

unsupported operand type(s) for -: 'str' and 'datetime.timedelta'

So I tried to convert it to an int but with no success.

Upvotes: 0

Views: 92

Answers (1)

neverwalkaloner
neverwalkaloner

Reputation: 47354

This part of code datetime.now().strftime("%Y-%m-%d") return formated datetime string. You can find strftime() description here. You need to substract timedelta first and then apply formatting to the result:

yesterday = (datetime.now() -  timedelta(days=1)).strftime("%Y-%m-%d")

Upvotes: 1

Related Questions