Python Forum
Can't substract seconds from time - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Can't substract seconds from time (/thread-25891.html)



Can't substract seconds from time - Raj_Kumar - Apr-15-2020

Hi

iam novice to python.I can't understand where i did the mistake & why. Kindly correct my code.

#Write a Python program to add 5 seconds with the current time.
import datetime
t1 = datetime.datetime.now().time()
print("Current time:", t1)
t=t1+datetime.timedelta(seconds = 5)
print("After 5 Seconds:", t)
When i execute the above code iam getting "TypeError: unsupported operand type(s) for +: 'datetime.time' and 'datetime.timedelta'".

Thank you


RE: Can't substract seconds from time - bowlofred - Apr-15-2020

Don't try to add time() objects. Instead do your math with datetime() objects and then convert to time for display if you need.

#Write a Python program to add 5 seconds with the current time.
import datetime
t1 = datetime.datetime.now()  # stays a datetime object
print("Current time:", t1.time())
t=t1+datetime.timedelta(seconds = 5)
print("After 5 Seconds:", t.time())