Home > Blockchain >  AttributeError: 'str' object has no attribute 'timedelta'
AttributeError: 'str' object has no attribute 'timedelta'

Time:03-04

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'

CodePudding user response:

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

CodePudding user response:

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.

  • Related