Page 1 of 1
Adding decimal hours to a date
Posted: Sat Oct 13, 2018 9:28 am
by TawnyOwl
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 ?
Re: Adding decimal hours to a date
Posted: Sat Oct 13, 2018 9:52 am
by hippy
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.
Re: Adding decimal hours to a date
Posted: Sat Oct 13, 2018 11:58 am
by TawnyOwl
Yes, thank you. It seems the the decimal number is in days, not hours.
Re: Adding decimal hours to a date
Posted: Sat Oct 13, 2018 12:16 pm
by hippy
+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.