Python Forum
Can't substract seconds from time
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can't substract seconds from time
#1
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
Reply
#2
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())
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem with module time and leap seconds Pedroski55 3 1,189 Oct-07-2022, 11:27 PM
Last Post: Pedroski55
  Store variable data and display sum after 60 seconds the_dude 11 3,367 Dec-16-2021, 07:07 PM
Last Post: deanhystad
  How to calculate time difference between each row of dataframe in seconds Mekala 1 2,516 Jul-16-2020, 12:57 PM
Last Post: Larz60+
  Need to add hours min and seconds tester_V 5 3,029 Jun-02-2020, 05:29 PM
Last Post: tester_V
  How to calculate time in seconds rajeshE 1 2,078 Feb-15-2020, 11:07 PM
Last Post: Larz60+
  how to stop and start a script for 30 seconds laspaul 9 7,512 Jan-16-2020, 02:13 PM
Last Post: laspaul
  Convert HH:MM:SS to seconds RavCOder 4 9,832 Nov-26-2019, 03:46 PM
Last Post: DeaD_EyE
  pyjama - frame every x seconds? MuntyScruntfundle 2 2,420 Jan-18-2019, 05:18 PM
Last Post: MuntyScruntfundle
  Conversion of Time Duration in seconds in python jdevansh99 0 2,828 Jun-05-2018, 05:12 PM
Last Post: jdevansh99
  starting a process every 10 seconds Skaperen 7 15,105 Dec-31-2017, 03:47 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020