TawnyOwl
Posts: 7
Joined: Fri Jun 22, 2018 12:22 pm

Adding decimal hours to a date

Sat Oct 13, 2018 9:28 am

Hello,

In need to make the sum of a date and a number (hours in decimal).

Example :
Date = 2019/12/31 16:09:51
Hours = 0.3732946033

Sum needs to be : 2019/12/31 20:38:37

How can I do this using datetime ?

hippy
Posts: 7911
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Adding decimal hours to a date

Sat Oct 13, 2018 9:52 am

0.3732946033 hours seems to me to be 22 minutes 22 seconds, so adding to "2019/12/31 16:09:51" would seem to give "2019/12/13 16:32:13", not as you suggested.

I can't remember how to do it exactly, but convert the date and time to seconds since epoch, convert hours to seconds, add the two, then convert back to a date.

TawnyOwl
Posts: 7
Joined: Fri Jun 22, 2018 12:22 pm

Re: Adding decimal hours to a date

Sat Oct 13, 2018 11:58 am

Yes, thank you. It seems the the decimal number is in days, not hours.

hippy
Posts: 7911
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Adding decimal hours to a date

Sat Oct 13, 2018 12:16 pm

+0.37 days is about +9 hours, 2019/12/31 16:09:51 + 9 is the day after, 2020/01/01 01:09:51

But I'm sure you'll figure it out as you go.

Return to “Python”