简体   繁体   中英

Formula to find difference in time between two values when they pass midnight?

I have a series of times and I am trying to find the difference in time between two values which is fine for a normal day however some times pass midnight eg

18:00 - 00:30

The time difference is 6 hours 30 minutes which I would like returned in a numerical value eg 6.5 hours. However whenever I pass midnight it is not calculating properly.

Is there anyway to work around this without having to input the dates on each value? I would like it to assume if I am between the hours of 00:00 & 08:00 then I mean the following morning and anything between 08:01 and 23:59 is from the previous day.

Use this formula:

=IF(A1>B1,1,0)+B1-A1

在此处输入图片说明

To get the 6.5 just multiply by 24, and make the format general:

=(IF(A1>B1,1,0)+B1-A1)*24

在此处输入图片说明


Edit #2

We can get rid of the IF as the expression (A1>B1) will return 1/0.

在此处输入图片说明

=((A1>B1)+B1-A1)*24

Try it as,

=ABS(SUM(A2, -(B2+(B2<TIME(8, 0, 0)))))*24

... which leaves 6.5 as 6 (integer) representing hours and .5 (decimal) representing 30 minutes.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM