Calculating sleep duration (before and after midnight) in Google Sheets - google-sheets

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.
Here is what I have so far. The formula on F2 works just fine until you cross midnight in C2. For example, if B2 is 9pm and C2 is 11pm, F2 is 120minutes. Once you change C2 to 12pm (0:00 in 24hour notation), the calculation flips and becomes 540minutes.
I'm thinking since the cells like to count backwards, I could maybe take 24 hours and subtract it by the value in F2, effectively getting back the other portion?
I stumbled into a weird pseudo-fix: if B2 is 21:00 (9pm) and I put 30:00 into C2, C2 displays 6:00 and F2 counts properly. Here is what that looks like. I am hoping to find a better solution than this-- the UX i'm looking to achieve is just putting my bed time and my wake time and the cells do the rest.
Thanks in advance!!

Drop the abs.
C2-B2
already assumes next day when C2<B2 as shown here:
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 timevalue(). With that, the underlying value will be converted as described above.
To then display minutes, you can format the cell to display Elapsed minutes using the steps shown below.
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

What formula will sumif a time range is between midnight and 8am?

I need to sum values in c3:c15 if v3:v15 is between 8am and midnight.
enter image description here
try:
=INDEX(SUM(IF((TIMEVALUE(V3:V15)>=0.33)*(TIMEVALUE(V3:V15)<=0.999), C3:C15, )))
In Google Sheets the value that represents midnight is 0. Taking into account that the time of the day uses arithmetic modulo 24 (1 in sheets), you only need to check if the time is bigger or equal to 8AM (as any time over 12PM will, in fact, be less than 8 AM). Therefore this can be simplified into a single SUMIF:
=SUMIF(V3:V15, ">=8:00", C3:C15)
References
SUMIF (Google Editors Help)
Modular arithmetic (Wikipedia)

google-sheets-if-and-formula-between-two-times return result or return original result

Spreadsheet
I need to show the result of altering energy use during peak price times on the cost of energy over the year. To do this I want to change the amount of energy being used, value in G6 to be 8 if the time at B6 is between the morning or afternoon peak pricing times, or keep the value in C6 if B6 is not within the peak pricing times.
Morning Peak Start 06:30:00 Finish 07:45:00
Afternoon Peak Start 16:30:00 Finish 23:45:00
I have attempted to modify the code found here:
excel-if-and-formula-between-two-times
=IF(and(B6>$N$3,B6<$O$3),"8",IF(AND(B6>=$P$3,B6<$Q$3),8,C6))
however, this does not return "8" in the desired time ranges.
You can see in the Google Spreadsheet that I have experimented with code in N6,N7 and N8.
I appreciate your assistance.
Please examine your cells N3 and P3. They have an extra 05/01/1900 in them. Remove that, leaving the time part, and it should work fine.
The problem is that you were doing a comparison on a time string, e.g. 06:45:00 and a full date string, e.g. 05/01/1900 06:30:00, which results in a faulty comparison.
The values you analyse in a comparison must have the same data type and must have a consistent data within this data type. In your case both B6, N3,O3,P3 and Q3 are set to the data type Time. However, you are using the format Date Time in all the cells but B3. As you want to compare dates with time you must set all of these cells with the data type Date Time.
To do so, in your Spreadsheet's menu bar, after selecting all these cells select Format->Number->Date Time and add the right date to B6.

adding months to date, fraction

Looking around i see the Edate formula to add months, but im working with fractions. eg 2.8. I'm looking to add not only the 2 months, but also the days roughly; more to get a idea how close day wise it is to the next month.
Break down; the fraction is gained from dividing the reserve fund by the monthly loss. Im then looking to take the start of the current month and adding the fraction in months/days to get an idea how much further the reserve will last.
Thanks for your time.
With 2.8 in A1 please try:
=edate(eomonth(today(),-1),A1)+mod(A1,1)*365.24/12
eomonth(today(),-1) saves entering the start of the current month. As you know the edate part will move forward from the start of the current month by the number of months of the integer part [2] of A1. mod(A1,1) then takes the decimal part of A1 [0.8] for multiplication by 365.24/12 (the approximate number of days, on average, in a month) to move further forward by adding that number of days to the result of edate.

Google Sheets - Chart Duration Error

I am trying produce a burn chart in google sheets to record time remaining on a project, which has to be formatted as 'duration' but any data over 24 hours plots incorrectly.
It seems to be wrapping values over 24 hours back on themselves, so for example 30:00:00 plots at 06:00:00 (which is 30-24). So it seems to be treating the 'duration' as if they're numbers on a clock when I need them to work as normal numbers - i.e. go over 24 hours.
Here's the Sheet
and a screenshot:
Can anyone help?
It seems you found a bug in Google Sheets, googling for the problem finds other people with the same issue, e.g. here: https://productforums.google.com/forum/#!topic/docs/tf_ehe6J-CU
The workaround is to multiply the values by 24 to get the hours as a number. It will not be perfect, as it will show fractions of an hour after the dot, instead of proper minutes (you could implement a text formula to get the minute values e.g. as data point values if you really want to, but probably not on the Y axis). For most real life projects I'm guessing that should be good enough.

How do I add duration to a time in Google Sheets?

I've found a few answers to questions similar to this elsewhere, except with adding or subtracting two times to get a duration, however, I haven't been able to figure it out enough to meet what I want.
I'm trying to get the time that I woke up using the time that I went to bed and the duration. I've tried doing [Time] + [Duration], however when I do that I get a value like 10060:25:00. I've also tried adding the hour value of the duration and the hour value of the time and dividing by 24, but that didn't work either. I did figure out the minutes though, all I did was =MOD([Time minute value]+[Duration minute value],60).
So to clarify, I just need to find the hour value now.
I am not exactly sure if this answers your question, but I think it is very similar. And I apologise in advance for an unnecessarily complicated description.
Sorry I didn't have time to provide a shorter answer.
I wanted to create a run sheet for a workshop with session durations.
I have a start time, and each session has a duration. I can then calculate start time for next session and cumulative duration of the sessions.
column label :> A || B || C || D
Heading label:> start_time || duration || hidden_duration || Elapsed
Number format:> date/time || number || duration ||duration
new start_time=previous_start_time+hidden_duration
hidden_duration=if(isblank(duration),"0:00:00","0:"&duration)
It seems time is particular about how it interprets a number. This is where the duration format fits in, as it formats correctly to add to time. However, a field formatted as a "duration number format", must then be entered as a duration. In other words, it is expected as 00:00:00.000 which is very particular. Or in fact, it appeared it at least needed 00:00 i.e. Hours and minutes separated by colon.
As I only had minutes, I didn't want to have to enter in this particular format all the time; and if I was to put 5 in the duration field, it was being interpreted as 5 milliseconds (or something). So I have used a simple number in the duration field and inserted it into a string in the hidden_duration field to form 0:duration. For example to add 5 into the minutes column of the hidden_duration entry it would be "0:"&5. The if(isblank(),...test was necessary to avoid blank durations voiding all the way down the column. i.e. If duration was blank, add 00:00:00 time to the start_time.
Start_time Duration Hidden_duration Elapsed
08:30:00 15 =if(isblank(B2),"0:00:00","0:"&B2&":00") =C2
=A2+C2 5 =if(isblank(B3),"0:00:00","0:"&B3&":00") =D2+C3
Drag the second row down to repeat however many entries you will have.
Don't forget to make the appropriate columns the correct number formats.
Also, you can modify the duration formats using Format | Number | More formats| Custom Number Formats and removing the unwanted display fields (such as seconds).
Then, I also made some conditional formatting rules, so that if a duration was blank, the Elapsed and time cells were white text on white background so they looked blank.
For your application you could do the same but have separate hours and minutes columns to make for easy data entry, both being used in the "hidden_duration" formula.
I hope I have helped here, as I found your question, trying to do the same and Googling an answer, but then ended up experimenting and discovering some new things myself.
Someone more knowledgable may be able to correct my entry or make more efficient. {Or at least explain more efficiently (; }
=HOUR(B2-A2)+(MINUTE(B2-A2)/60)
Basically, being B2 the end time and A2 the start time, you subtract B2-A2 and get the hour from the subtraction. Then you do the same and get the amount of minutes. After you have both, you sum the amount of hours with the amount of minutes divided by 60 (to get it in hours).
Don't forget to use the 24-hour format (i.e. 10:00 or 22:00).

Resources