-1

I have a datetime variable and I want to substract just 1 hour from it. I tried to do it with below code but I received the following error: TypeError: unsupported operand type(s) for -: 'str' and 'datetime.timedelta'

from datetime import datetime, timedelta

cur_time = datetime.now()
cur_time_f = cur_time.strftime("%Y-%m-%dT%H:%M:%SZ")  
print(cur_time_f)

>> 2020-09-15T09:07:44Z

nueve = cur_time_f - timedelta(hours=1)

print(nueve)

My expected output:

>> 2020-09-15T08:07:44Z
NorthAfrican
  • 135
  • 2
  • 10

1 Answers1

1

Do the calculation BEFORE turning it into a string

from datetime import datetime, timedelta

cur_time = datetime.now()
print(cur_time)

>>2020-09-15 11:29:51.756391

cur_time = cur_time - timedelta(hours=1)
cur_time_f = cur_time.strftime("%Y-%m-%dT%H:%M:%SZ")  
print(cur_time_f)

>>2020-09-15T10:29:51Z
salih2012
  • 26
  • 6