Home > Software engineering >  Calculating sleep duration (before and after midnight) in Google Sheets
Calculating sleep duration (before and after midnight) in Google Sheets

Time:07-13

I am trying to calculate the total number of minutes spent in bed overnight by inputting the time entered bed (e.g. 9pm) and time exited bed (e.g. 6am). Using those example inputs, the answer should be 9hours, or 540minutes (9*60). Instead, my formula counts backwards, grabbing the duration from 6am to 9pm which is 15hours, or 900min.

The reason it works is that Google Sheet treats those as (duration of) time. And when displaying time, the "minus amount" is converted to a positive time value that represents a time of the previous day, as if modulus 24 hours. For example, 7:00 (hour) minus 21:00 (hour) gives -14:00 (hour) and it will be displayed as 24:00 (hour) - 14:00 (hour) = 10:00 (hour). Thus it is equivalent to the number you are looking for. However, modulus 24 hours effect is only for display. The minus sign is still inherent to the resultant value.


In order to display minutes or do other calculations with the result, you can wrap it with

And you will see minutes in the cell as shown below.


Your question implies that you may desire to do calculations on the result.

If that is that case, it helps to know that the output of timevalue is time duration in (fractions of) days without any inherent formatting. If not, you may skip the followings.

For example, in the preceding image, if you put =C1*2 in D1, with automatic formatting, D1 will inherit the format of C1, which was Elapsed minutes. You will thus see 1440. However, the actual underlying value is 1 because the real meaning of the data is half a day times 2. And you can confirm that by setting the format of D1 as numbers.

Thus, if you are only doing calculations with outputs of timevalue, you are fine without worrying about the above. However, if you mix the result of timevalue with other types of data, the fraction of day meaning is what will be used in your calculations.

  • Related