0
from datetime import datetime, time, timedelta

att1 = datetime.strptime('09:00 AM','%H:%M %p')

att2 = datetime.timedelta(minutes = 15)

time_zero = datetime.strptime('00:00 AM','%H:%M %p')

att3 =  (att1 - time_zero + att2).strftime("%X %p")

print(" sTime and Approx time is",att3)

If I removed datetime from att2, it is effected to the att3 att3 = (att1 - time_zero + att2).strftime("%X %p") AttributeError: 'datetime.timedelta' object has no attribute 'strftime'

Error

Traceback (most recent call last):

File "addtwotimes.py", line 5, in

att2 = datetime.datetime(minutes = 15)

AttributeError: type object 'datetime.datetime' has no attribute 'timedelta'

Vishnu VK
  • 1
  • 4
  • Well you imported timedelta you can use it just as `timedelta`. You use `datetime.timedelta` when you do `import datetime` – palvarez Sep 27 '19 at 09:59
  • Change `att2 = datetime.timedelta(minutes = '15')` to `att2 = timedelta(minutes = '15')` as you already imported timedelta as `from datetime import timedelta` – Rahul Raut Sep 27 '19 at 09:59
  • 2
    Possible duplicate of [type object 'datetime.datetime' has no attribute 'datetime'](https://stackoverflow.com/questions/12906402/type-object-datetime-datetime-has-no-attribute-datetime) – Sundeep Pidugu Sep 27 '19 at 09:59
  • @RahulRaut: it has to be `att2 = timedelta(minutes=15)` (int, not string). The OP already corrected the type though. – FObersteiner Sep 27 '19 at 10:01
  • Also, `att3 = (att1 - time_zero + att2).strftime("%X %p")` wont work since a timedelta has no method `strftime()`. Formatting timedeltas is a bit more difficult, see e.g. [here](https://stackoverflow.com/questions/538666/format-timedelta-to-string) – FObersteiner Sep 27 '19 at 10:06
  • Possible duplicate of [Adding two different times (Nothing showing when i print this python code)](https://stackoverflow.com/questions/58128110/adding-two-different-times-nothing-showing-when-i-print-this-python-code) – FObersteiner Sep 27 '19 at 10:10
  • Also, att3 = (att1 - time_zero + att2).strftime("%X %p") wont work since a timedelta has no method strftime() – Vishnu VK Sep 27 '19 at 10:12

0 Answers0