EXCEL: what is the formula for getting the time remaining until tonight 00:00 - excel

I have a list of date and time, say 3/5/2018 15:00:00
I want a formula to calculate the time remaining until tonight 00:00
for this example, it should be 09:00:00
this is an image of the resulting table I wanted
what i tried: =TEXT(TIME(24,0,0)-A20, "hh:mm") the result is #Value!

The general algorithm for this, in any programming language or environment is simple:
Get the date component from the date-time value.
Increment the day value.
Subtract the date-time value from the date value in step 2
This works because the value from step 2 is still a date-time value, but the time-of-day component is now zero which represents that day's morning-midnight (i.e. 00:00 in the 24-hour clock).
In Excel, date, time, and date-time values are all represented by decimal numbers where the integer component represents a day-count after some epoch and fractional component represents the time-of-day.
This means step 1 is done by simply doing INT( date-time-value ).
And step 2 is done by simply adding +1 to that value.
And step 3 is simple subtraction, but be sure to set the cell's display format to "Time" and not Date or Date-Time.
Cell B2 should have this formula:
=( INT( A2 ) + 1 ) - A2
Then drag-fill that for the column to get the values for the other rows.
That's it.

So, did this, do note that my answer is in decimals... You can sort to hours, minutes and seconds...
=24-(HOUR(B4)+MINUTE(B4)/60+SECOND(B4)/3600)

This should do:
=24-TEXT(A20,"hh:mm")
Considering midnight is either 0 or 24, depending on where you come from, as we are moving towards it we consider as "24" and when we are moving from it, "0". So I subtract the current time/date in a format that excludes the date from "24" and there you have remaining time to midnight.
Do not forget to format the cell to "time", otherwise the results will be wrong.

Related

Excel hours worked calculation, based on specific time shifts

I have an excel with working hours in format Starting: 15:30 (A1) and finishing: 01:00 (B1). I would like to calculate the hours that i worked as nightshift (C1). The night shift is between 22:00 and 06:00. So if i work for example 17:00 to 02:15, i should get the number 04:15 as nightahift hours. I i work from 22:00 to 09.15 i should get 8 nightshift hours. I got the time difference with this
""=MOD(B2-A2;1)"" but this gets the whole shift and not the night ones only. Any suggestions?
It seems that this code works for one pair of columns, but i cannot figure out how to sum the results. The excel is seperated into columns, where every date has 2 columns for Starting and Ending shift
=MOD(D4-C4;1)24-(D4<C4)(22-6)-MEDIAN(D424;6;22)+MEDIAN(C424;6;22)
The ideal would be to have a formula to this result
=MOD(D4-C4;1)24-(D4<C4)(22-6)-MEDIAN(D424;6;22)+MEDIAN(C424;6;22)+
MOD(F4-E4;1)24-(F4<E4)(22-6)-MEDIAN(F424;6;22)+MEDIAN(E424;6;22)+
MOD(H4-G4;1)24-(H4<G4)(22-6)-MEDIAN(H424;6;22)+MEDIAN(G424;6;22) etc, but more compact way
Assuming that all input times are relative to 1/0/1900 (only time portion information). Therefore you need to consider when the date refers to the next day (adding 1). The condition for the end date is that if start > end, then the end date refers to the next day (+1). We take into account the above for the solution.
Assuming no excel version constraints as per the tags listed in the question, you can use the following formula in F2:
=LET(start, A2:A5, end, B2:B5, ns, D2, ne, D3+1, eEff, IF(end<start, end+1, end),
CALC, LAMBDA(isStart, MAP(start, eEff, LAMBDA(x,y, IF(y < ns, 0,
MEDIAN(IF(isStart, x, y), ns, ne))))), a, CALC(TRUE), b, CALC(FALSE),b-a)
I created a user LAMBDA function CALC just to avoid repetition of a similar calculation for the actual start (a) and end (b) of the working night interval. As you can see from the formula the night end (ne) has been adjusted to consider the next day. Similarly, we do the adjustment for the end time, calculating the effective end time (eEff) considering the next day when the end time (end) is lower than the start time (start).
We use MEDIAN to identify the actual interval in both cases. Here is the output:
where 0:00 means no time consumed during the night shift. The rest is just to use the correct format: hh:mm in the output to show durations during the night shift.
If you show the input data in numeric format, you can see the times referred to the first valid year in Excel (<= 1):
That is why we need to convert ne and end (in case it represents the next day), but it depends on whether or not you want to consider the next day in your input data. So for row 2 it represents 01:00 of the current day (0.04) so in the formula, we treat it as: 01:00 of the next day (1.04)

Is there any function in excel to find day time between two date and time in Excel?

I need a formula to calculate between two date and time excluding lunch time, holidays, weekend and between 09:00 and 18:00 work hours.
For example, 25/07/2022 12:00 and 29/07/2022 10:00 and answer has to be 1 day, 06:00
Thanks in advance.
I had a formula but it didn't work when hours bigger than 24 hours.
I don't know how you got to 1 day and 6 hours, but here is a customizable way to filter your time difference calculation:
=LET(
start,E3,
end,E4,
holidays,$B$3:$B$5,
array,SEQUENCE(INT(end)-INT(start)+1,24,INT(start),TIME(1,0,0)),
crit_1,array>=start,
crit_2,array<=end,
crit_3,WEEKDAY(array,2)<6,
crit_4,HOUR(array)>=9,
crit_5,HOUR(array)<=18,
crit_6,HOUR(array)<>13,
crit_7,ISERROR(MATCH(DATE(YEAR(array),MONTH(array),DAY(array)),holidays,0)),
result,SUM(crit_1*crit_2*crit_3*crit_4*crit_5*crit_6*crit_7),
result
)
Limitation
This solution only works on an hourly level, i.e. the start and end dates and times will only be considered on a full hour basis. When providing times like 12:45 as input, the 15 minute increment won't be accounted for.
Explanation
The 4th item in the LET() function SEQUENCE(INT(end)-INT(start)+1,24,INT(start),TIME(1,0,0)) creates an array that contains all hours within the start and end date of the range:
(transposed for illustrative purposes)
then, based on that array, the different 'crit_n' statements are the individual criteria you mentioned. For example, crit_1,array>=start means that only the dates and times after the start date and time will be counted, or crit_6,HOUR(array)<>13 is the lunch break (assuming the 13th hour is lunch time), ...
All of the individual crit_n's are then arrays of the same size containing TRUE and FALSE elements.
At the end of the LET() function, by multiplying all the individual crit_n arrays, the product returns a single array that will then only contain those hours where all individual criteria statements are TRUE:
So then the SUM() function is simply returning the total number of hours that fit all criteria.
Example
I assumed lunch hour to be hour 13, and I assumed the 28th to be a holiday within the given range. With those assumptions and the other criteria you already specified above, I'm getting the following result:
Which looks like this when going into the formula bar:
In cell G2, you can put the following formula:
=LET(from,A2:A4,to,B2:B4,holidays,C2:C2,startHr,E1,endHr,E2, lunchS, E3, lunchE, E4,
CALC, LAMBDA(date,isFrom, LET(noWkDay, NETWORKDAYS(date,date,holidays)=0,
IF(noWkDay, 0, LET(d, INT(date), start, d + startHr, end, d + endHr,
noOverlap, IF(isFrom, date > end, date < start), lunchDur, lunchE-lunchS,
ls, d + lunchS, le, d + lunchE,
isInner, IF(isFrom, date > start, date < end),
diff, IF(isFrom, end-date-1 - IF(date < ls, lunchDur, 0),
date-start-1 - IF(date > le, lunchDur, 0)),
IF(noOverlap, -1, IF(isInner, diff, 0)))))),
MAP(from,to,LAMBDA(ff,tt, LET(wkdays, NETWORKDAYS(ff,tt,holidays),
duration, wkdays + CALC(ff, TRUE) + CALC(tt, FALSE),
days, INT(duration), time, duration - TRUNC(duration),
TEXT(days, "d") &" days "& TEXT(time, "hh:mm") &" hrs "
)))
)
and here is the output:
Explanation
Used LET function for easy reading and composition. The main idea is first to calculate the number of working days excluding holidays from column value to to column value. We use for that NETWORKDAYS function. Once we have this value for each row, we need to adjust it considering the first day and last day of the interval, in case we cannot count as a full day and instead considering hours. For inner days (not start/end of the interval) it is counted as an entire day.
We use MAP function to do the calculation over all values of from and to names. For each corresponding value (ff, tt) we calculate the working days (wkdays). Once we have this value, we use the user LAMBDA function CALC to adjust it. The function has a second input argument isFrom to consider both scenarios, i.e. adjustment at the beginning of the interval (isFrom = TRUE) or to the end of the interval (isFrom=FALSE). The first input argument is the given date.
In case the input date of CALC is a non working day, we don't need to make any adjustment. We check it with the name noWkDay. If that is not the case, then we need we need to determine if there is no overlap (noOverlap):
IF(isFrom, date > end, date < start)
where start, end names correspond to the same date as date, but with different hours corresponding to start Hr and end Hr (E1:E2). For example for the first row, there is no overlap, because the end date doesn't have hour information, i.e. (12:00 AM), in such case the corresponding date should not be taken into account and CALC returns -1, i.e. one day needs to be subtracted.
In case we have overlap, then we need to consider the case the working hours are lower than the maximum working hours (from 9:00 to 18:00). It is identified with the name isInner. If that is the case, we calculate the actual hours. We need to subtract 1 because it is going to be one less full working day and instead to consider the corresponding hours (that should be less than 9hrs, which is the maximum workday duration). The calculation is carried under the name diff:
IF(isFrom, end-date-1 - IF(date < ls, lunchDur, 0),
date-start-1 - IF(date > le, lunchDur, 0))
If the actual start is before the start of the lunch time (ls), then we need to subtract lunch duration (lunchDur). Similarly if the actual end is is after lunch time, we need to discount it too.
Finally, we use CALC to calculate the interval duration:
wkdays + CALC(ff, TRUE) + CALC(tt, FALSE)
Once we have this information, it is just to put in the specified format indicating days and hours.
Now let's review some of the sample input data and results:
The interval starts on Monday 7/25 and ends on Friday 7/29, therefore we have 5 working days, but 7/26 is a holiday, so the maximum number of working days will be 4 days.
For the interval [7/25, 7/29] starts and ends on midnight (12:00 AM), therefore the last day of the interval should not be considered, so actual working days will be 3.
Interval [7/25 10:00, 7/29 17:00]. For the start of the interval we cannot count one day, instead 8hrs and for the end of the interval, the same situation 8hrs, so instead of 4days we are goin to have 2days plus 16hrs, but we need to subtract in both cases the lunch duration (1hr) so the final result will be 2 days 14hrs.

SUM interval of rows even if error

i'm trying to sum values but when a cell as an error, id like that to count as a 0, and continue adding.
the values in the cells are 15:60, or 8:00, "h:mm"
i've tried this:
{=SUM(IF(ISERROR(A1:A7);0;A1:A7))}
but it gives me a weird value
example:
15:00
06:00
gives me 0,875?
i just want it to add, 15:30 + 15:30, should give 31:00
thx in advance!
[SOLVED] change the cell type to TIME 37:30:55
21/24 = 0.875
The result is 21:00 but in float number, the normal way for Excel to store time.
The same with date times, they are also stored that way.
Integer (whole numbers) as the number of days since 1/1/1900 and the fraction as the time.
Most likely all you need to do is to change the format of the cell to time and it will be displayed correct.

Excel Formula - get the date of a maximal value within a list

I have a rather big (long) table and need to do something quite simple but I'm currently with a sort of blackout...
Imagine that you have:
Date 1 Value 1 Date 2 Value 2 Date 3 Value 3 Date of MAX
... ... ... ... ... ... ????
I want to deploy in ???? a formula that will result in the date at which the maximal value (between Value 1...3) was registered. For instance:
Date 1 Value 1 Date 2 Value 2 Date 3 Value 3 Date of MAX
20160501 10 20160722 47 20161002 9 20160722
meaning, the Date of MAX is of Date 2 because that was the date at which the MAX was measured.
Thanks in advance.
You could do a more general solution using offset:-
=MAX(N(OFFSET(D2,0,COLUMN(A:D)*3)))
to find the largest value - put this in (say) R2.
Then find a match for it and pick out the corresponding date:-
=OFFSET(C2,0,MATCH(R2,N(OFFSET(D2,0,COLUMN(A:D)*3)),0)*3)
assuming the dates and values are every third column.
These are array formulae and must be entered with CtrlShiftEnter
If the last value really was in P21 you would have to give a row offset as well as a column offset.
OK, I found a dirty but simple solution (don't know why I didn't think of it at first):
=IF(G2>=MAX(G2,J2,M2,P21),F2,IF(J2>=MAX(G2,J2,M2,P21),I2,IF(M2>=MAX(G2,J2,M2,P21),L2,O2)))
where the pairs (4 of them) are at FG, IJ, LM, OP. Not elegant but does the job.

Adding/Subtracting Whole numbers from Time

I have tried every which way to format cells to subtract the result from time for instance the formula in the cell = 11(this is 11 minutes) I want to take that result minus 8:00:00 to give me 7:49:00 but it doesn't work the result is ####### no matter how big I make the cell. And if I format the cells with the formula to custom [m]:ss then the value changes.
Sample of the Worksheet:
I want Y2 = X3-W3 in a time format.
So, if A1=11
Then in some other cell, (B1 in this example): =TIME(,A1,)
Then subtract from the cell with 8:00:00. (If it's C1...:)
=C1-B1
That will give you the time you want.
Info: The main thing is that you have to tell Excel that your cell with the "11" in it, is minutes. By using the =TIME(,A1,) you will get the value of: 12:11 am. (If you keep it in Date format.) 12:11 am could also be viewed as: 0 Hours, 11 minutes, 0 seconds. And now that it knows, you should be able to subtract.
Try this:
=TIME(HOUR(X3),MINUTE(X3)-W3,SECOND(X3))
The ######### is because you have a negative time. Becuase Excel reads time as decimals of 24 hours, 8:00:00 is .3333 and if you subtract 11 from that you get -10.6666 and date/time can not be negative.

Resources