Aaron88
Aaron88

Reputation: 187

AttributeError: 'str' object has no attribute 'timedelta'

How do I do minus 30 days to the following time.

datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%dT%H:%M:%S") - datetime.timedelta(30)

The above prints the following error:

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

Upvotes: 0

Views: 755

Answers (2)

kenshima
kenshima

Reputation: 583

Do strftime after doing the date calculation

import datetime
(datetime.datetime.now(datetime.timezone.utc)- datetime.timedelta(30)).strftime("%Y-%m-%dT%H:%M:%S")

As mentioned in the comments strftime converts a datetime to a string which is why you can't use timedelta on it.

Upvotes: 2

Marya
Marya

Reputation: 180

I think this is correct form of what you need:

from datetime import datetime , timedelta
(datetime.now(datetime.timezone.utc) - timedelta(30) ).strftime("%Y-%m- 
  %dT%H:%M:%S")

Upvotes: 2

Related Questions