John Titus Jungao John Titus Jungao - 1 year ago 117
Python Question

Python: Adding Float formatted time to datetime value

I want to add a randomly generated time value to an existing

object. Now, the time value that I have is a float. For example, I want to add
hours to
. How do I do this? Thanks.

Answer Source

You just need to construct a timedelta and add it:

>>> import datetime
>>> d = datetime.datetime(2016, 11, 17)
>>> delta = datetime.timedelta(hours=4.1326742)
>>> d + delta
datetime.datetime(2016, 11, 17, 4, 7, 57, 627120)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download