How do I add n seconds to datetime.time?
11:34:59 + 3 = 11:35:02
I wonder how to use it if I want to add it right away. I tried this way, but it didn't work, so I'm not sure what to do ㅜ<
import datetime
mytime = datetime.time(11, 34, 59)+3
print(mytime)
mytime = datetime.time(11, 34, 59)+3
TypeError: unsupported operand type(s) for +: 'datetime.time' and 'int'
import datetime
mytime = datetime.time(11, 34, 59)+datetime.timedelta(0,3)
print(mytime)
mytime = datetime.time(11, 34, 59)+datetime.timedelta(0,3)
TypeError: unsupported operand type(s) for +: 'datetime.time' and 'datetime.timedelta'
import datetime
mytime = datetime.time(11, 34, 59)+datetime.time(0,0,3)
print(mytime)
mytime = datetime.time(11, 34, 59)+datetime.time(0,0,3)
TypeError: unsupported operand type(s) for +: 'datetime.time' and 'datetime.time'
Normal
Run in the same step.
import datetime
a = datetime.datetime(100,1,1,11,34,59)
b = a + datetime.timedelta(seconds=3)
print a.time()
print b.time()
Result:
11:34:59
11:35:02
To define a function in a more manual way,
import datetime
def addSecs(tm, secs):
fulldate = datetime.datetime(100, 1, 1, tm.hour, tm.minute, tm.second)
fulldate = fulldate + datetime.timedelta(seconds=secs)
return fulldate.time()
a = datetime.datetime.now().time()
b = addSecs(a, 300)
912 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
581 PHP ssh2_scp_send fails to send files as intended
572 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
617 Uncaught (inpromise) Error on Electron: An object could not be cloned
© 2024 OneMinuteCode. All rights reserved.