Calculate time difference between NOW() and a calculated time - excel-formula

I have a cell that simply looks like 4:11 PM. Here is the formula that calculates it:
[Time In]+((TIME(8,0,0)-(K11/24))+([Lunch End]-[Lunch Start]))
I can go into that in more detail, but hopefully it's not necessary.
Now, what I want to do is display what is essentially a countdown, the difference between NOW() and 4:11 PM. Here is the closest I came:
=HOUR(NOW())-HOUR(Table1[#[Leave At]])&":"&TEXT(MINUTE(NOW())-MINUTE(Table1[#[Leave At]]),"00")
This was right until the minutes passed the comparison minutes, then it started counting back up (IE at 2:05 it said 2:06 left, but once it was 2:25 it said 2:14 left). Sorry if this is not clear.
One of the problems is, if I try TIMEVALUE(L12) (L12 being 4:11 PM) I get #VALUE!. I'm assuming this is why it's not working, and why simply doing =L12-NOW() didn't work.
How can I solve this?

The issue is NOW() is a DATE/TIME so the numeric value is >42000. Wand when subtracting it from A time which is <0 you get a negative number, so NOW() needs to be changed to just its decimal part:
=L12-TIME(HOUR(NOW()),MINUTE(NOW()),0)
Or
=L12-(NOW()-INT(NOW()))
The second will return seconds with the output.

Related

Calculate if time period is contained within a start and end time

I have a problem where I am trying to calculate in Excel if any part of a provided work duty time period is contained within a user defined period which specifies the night working hours e.g. 2330-0559 or 0000-0630.
If I provide a work duty time I want and any part of the duty is within the specified period it needs to be identified. E.g. 2230-0630 duty time is within both examples above.
I can tried a few different solutions and still not got the right way solve it.
Maybe someone can help. I also know that 24:00 in excel is used for midnight at the end of the day and 00:00 is used for midnight starting the day as excel works from 0-1 as part of the day.
=OR(MOD(A5,1)>$E$2,MOD(A5,1)<$F$2)
I would work with the number of minutes as decimal and with it make a valid range from "Duty start time" and "Duty end time".
Check if you can make any sense out of this:
Here the example:
https://drive.google.com/file/d/1O1B76srlY8sYQHsV_TiZVzi-7B_18eSj/view?usp=sharing
Update: Sorry for the lack of explanation. Here I try my best to clarify how I did it.
Conversion
First of all I try to convert any time into minutes, so 00:00 is 0 minutes, 01:00 is 60 minutes, 24:00 (excel shows 00:00) is 1440 minutes, 25:00 (excel shows 01:00) is 1500 minutes.
That conversion I do with CONVERT(E2,"day", "mn"), which does convert from day to minutes.
Range Normalization and inclusion
Now every range has to be normalized, that means the "End" has to be always bigger than "Start". For 00:00 to 06:00 this works fine, but for 22:00 to 06:00 it needs to be tweaked. So if "Start" is bigger than "End" then I add 1440 mins (24 hours) to "End". That I do with IF(A6>B6, 1440,0).
You then need to see if any "Duty range" contains the "Night range". That normally is done with the formula if (DutyEnd > NightStart AND DuttyStart < NightEnd) then TRUE.
Challenge 1
That was the main concept. But then if you try to compare a range like 00:00-06:00 contained in 24:00-06-00 it does not work. And that is because the converted minutes are 0-360 and 1440-1830, they don't contain each other.
In E3 to fix this I cut down 1440 with modulo (MOD()), then MOD(1440, 1440) = 0. So even if you use values like 25:00 or 52:00, they will be cut down to the smallest amount of minutes. E.g. 25:00 (1500 mins) = 1:00 = 60 mins, 52:00 (3120 mins) = 4:00 = 240 mins
Challenge 2
We have yet another challenge, the possible comparisons are as follows:
00:00-06:00 contained in 00:00-06:00 which in minutes is 0-390 contained in 0-390
00:00-06:00 contained in 22:00-06:00 which in minutes is 0-390 contained in 1320-1830
22:00-06:00 contained in 00:00-06:00 which in minutes is 1320-1830 contained in 0-390
These last two will not match. So that is why in the "Contains" column (E.g.: E6) I compare against the "Night criteria" - 1400 and + 1400.
Hope it is a bit clear. Let me know otherwise..
UNDERSTANDING DATE AND TIME IN EXCEL
Dates in excel are stored as INTEGERS. They represent the days since 1900/01/1 with that date being 1.
Time is stored as a decimal which represents a fraction of a day. 24 hours is 1, 0.5 is 12 noon. etc.
In other words, everything to the left of the decimal is date and everything to the right is time.
JUST USING TIME AND CROSSING MIDNIGHT
This is problematic from the view point that early morning times are less than the late times of the previous day. The fact of the matter is that they are larger. In our heads we do the mental math of knowing the are the following day but we ignore the date aspect.
A quick way to rectify this is to add the date to your time. Life will become much easier with the math. You may however not want to add full dates to start and end times. WITH THE ASSUMPTION that start and end times are not more than 24 hours the simple work around is is to add 1 to the end time when the end time is less than the start time. This means its the next day.
It the example date you provided, column C was insert to CORRECT the end time. It did the check of end less than start if so add one using the following formula:
=B6+(B6<=A6)
The part in brackets is a logic check. It either evaluates to TRUE or FALSE. When excel runs a boolean (TRUE or FALSE) through a math operator (not a function like sum) it will convert TRUE to 1 and FALSE to 0.
LENGTH
Straight forward math of C minus A since C is always after you start and is the larger of the two numbers.
=C6-A6
CROSSING MIDNIGHT
Need to be a little careful in your definition of crossing midnight when a start time or end time is exactly midnight. Technically speaking you did not cross it if you start or stop on it. The difference is really < versus <= or > versus >=. I will leave that to you to sort out. For the math I used:
=AND(A6<1,C6>=1)
Though I did not use this column for anything else
START CHECK
=OR(A6>$F$2,A6<$G$2)
END CHECK
=OR(B6>$F$2,B6<$G$2)
ANY TIME CHECK
I broke this into three columns. It can be combined into one but wanted to show the working parts. The first check is to see if the start time is before the night start and that the shift end time was after the night start time. The second check was similar for the the fisrt except you want to know if the start time is before the night end time and the shift end time is after the night end time. For the OR case you want to check to see if ANY of columns F through I are true:
COLUMN H
=AND(A6<=$F$2,C6>$F$2)
COLUMN I
=AND(A6<$G$2+1,C6>$G$2+1)
Note the +1 for night end time. This is to reflect that the end time is actually on the following day.
COLUMN J
=OR(F6,G6,H6,I6)
or
=(F6+G6+H6+I6)>=1
Place the above formulas in row 6 and copy down as needed
Well, I think I'm missing something but let's see if this works for you. To make this work:
All hours must be typed in hh:mm format
Night Criteria End must be on a different day. This means it must be over 00:00 or formulas won't work. If you type something
like start criteria=22:00 and night criteria= 23:50, both times
would be in the same day, so formulas won't work
This formulas only work in periods less than 24 hours. If anytime the different between criterias is over 24 hours, formulas won't work
properly.
Now, I replied your data like this:
The trick here is dealing with times without dates. Dates in Excel are înteger numbers and decimal parts are the hours/minutes/seconds. So to compare properly hours like this, you need to add an integer part.
Let me explain. In Excel, 18:00 would be 0,75. And 06:00 would be 0,25. If you try to get the difference between both times, you will get -0,5. In decimal it makes sense, but when trying to convert to time, it makes no sense for Excel. So, as I said before, the trick here is adding integers (in this case, because 06:00 is lower than 18:00, we would add +1), so Excel would make 1,25 - 0,75 = 0,5. And Excel can convert 0,5 to hours, and it will return exactly 12 hours, the right result.
So knowing this, the trick in formulas for your data is comparing ends with starts and add 1 or 2 to compare then properly with your criteria. That way Excel can figure out if a time is later or sooner than a criteria.
All my formulas are these ones:
LENGHT: =IF(B5<A5;B5+1-A5;B5-A5)
CROSS MIDNIGHT: =IF(Y(B5<A5;B5<>0);TRUE;FALSE)
START BETWEEN CRITERIA: =IF(AND(IF(A5<$E$2;A5+2;1+A5)>=1+$E$2;IF(A5<$E$2;A5+2;1+A5)<=$F$2+2);TRUE;FALSE)
END BETWEEN CRITERIA: =IF(AND(IF(B5<$E$2;B5+2;1+B5)>=1+$E$2;SI(B5<$E$2;B5+2;1+B5)<=$F$2+2);TRUE;FALSE)
ANY PART:=IF(OR(E5=TRUE;F5=TRUE;AND(A5<$E$2;A5+C5>1+$F$2));TRUE;FALSE)
Anyways, I uploaded a file to my Gdrive, in case it may be helpful to download and check the formulas.
This is the best I got. Probably there is a better way, but I hope this can help you, or at least, you can adapt it to your needs.
https://drive.google.com/open?id=1nrZKfyUhED_y6iiPRSUwRf7GhJlBYt-O

Excel IF AND formula between two times

I would like to have a formula which will tell me if a time in a cell is between 2 separate vlaues in other cells and to return a value if so.
I have already created the below code but this is not returning any values back at all.
=IF(AND(F4>=$R$1,F4<P1),"Night Shift",IF(AND(F4>=$P$1,F4<$Q$1),"AM Shift",IF(AND(F4>=$Q$1,F4<$R$1),"PM Shift","")))
In this example the cell values are (P1 = 06:00, Q1 = 14:00, R1 = 22:00). The value in the F4 is 00:31:38.
Any help would be appreciated.
Your first AND needs to adjust a little.
Excel sees TIME as a fraction of 1 whole day. So 00:31:38 though you meant it to be the next day from 22:00, Excel does not know that and as such will not see it greater than 22:00
We also do not need to test for the Night Shift. It is the only option left if the time is not in one of the others:
=IF(F4<>"",IF(AND(F4>=$P$1,F4<$Q$1),"AM Shift",IF(AND(F4>=$Q$1,F4<$R$1),"PM Shift","Night Shift")),"")
You could also create a small table like such:
0 6:00 14:00 22:00
Night Shift AM Shift PM Shift Night Shift
Then use a HLOOKUP to return the correct value:
=HLOOKUP(F4,O1:R2,2,TRUE)
I took a slightly different path that Scotts.
A Night Shift occurs if the time is greater or equal to 10PM, OR is less than 6AM.
=OR($F$4<$P$1,$F$4>=$R$1)
An AM Shift occurs when the time is greater or equal to 6AM, AND is less than 2PM.
=AND($F$4>=$P$1,$F$4<$Q$1)
A PM Shift occurs when the time is greater or equal to 2PM, AND is less than 10PM.
=AND($F$4>=$Q$1,$F$4<$R$1)
Stick the three conditions together and you have:
=IF(OR($F$4<$P$1,$F$4>=$R$1),"Night Shift",IF(AND($F$4>=$P$1,$F$4<$Q$1),"AM Shift",IF(AND($F$4>=$Q$1,$F$4<$R$1),"PM Shift","")))
Edit
During testing I entered 00:00:00 in A1 and =A1+TIMEVALUE("00:01:00") in A2:A1440.
At 06:00:00, 14:00:00 and 22:00:00 the changeover in shift happened a minute later.
If, however, I manually typed in 06:00:00 the changeover happened on the hour. This seems to be because TIMEVALUE is calculating 6AM as 0.2499999 rather than 0.25.
=IF(OR(HOUR(NOW())>22,HOUR(NOW())<7),"NIGHT","")&IF(AND(HOUR(NOW())>6,HOUR(NOW())<15),"MORNING","")&IF(AND(HOUR(NOW())>14,HOUR(NOW())<23),"LATE","")
I know a bit late but simplifies everything without the need of using other cells (note there are different times used).
Night - 23-07
AM - 07-15
PM - 15-23

How do i Subtract a duration from a time in Excel?

I am having an issue with Excel and it’s probably very simple but I need to get a starting time by subtracting a duration from and end time.
For example say I know that an event needs to end at 1:55:23 pm and it will take 0:22:13 what formula would I use to find out what time I should start?
I would like to be able to input the duration in the format h:mm:ss without excel trying to turn it into a time and not a duration as well.
Thank you for any suggestions
You simply subtract them.
For example...
Cell A1 has the end time: 1:55:23 PM
Cell B1 has the duration: 0:22:13 (this is the only 'strange' part because it's actually an AM time, as in minutes after midnight... but if you think about it, that's what you want).
Cell C1 has this formula: =A1-B1
That's it.
NOTE: If your duration is longer than your event start time (for example, a 4-hour event that started at 3:00 in the morning!) then the subtraction would result in a negative time.
A negative time is OK in reality, but Excel will not display it as a formatted time... instead it displays a bunch of ############. In that case, you need to display the calculation as a decimal value, which is the fraction of a day. For example. 0.25 means exactly 6 hours. If you would prefer decimal hours you simply multiply the fractional day figure by 24.

Excel time difference

I have two time:
9:29:00 AM (B2)
6:16:00 PM (C2)
I want to get the hh:mm (hours and minutes) between those two times. I tried the following function:
=TEXT(B2-C2,"h:mm")
But I get a #VALUE! error.
I formatted the two time cells as TIME format (*h:mm:ss am:pm) and result cell also as TIME format (hh:mm)
Is that the right way round? Try
=C2-B2
You don't need TEXT function
I believe your #VALUE error is due to your order of subtraction, but I can't be sure without knowing which of those times is B2 and which is C2, and I can't quite comment on your question yet, so an answer it is!
Times in excel are stored as doubles which increment by 1 each day; 0.5 is 12 hours etc.
Best thing to do is use =TEXT(B2-C2,"HH:MM")
This will format B2-C2 (a decimal number) as a time interval.
Of course the contents of B2 and C2 must be numeric; see my VALUE function in the comments.
As barry houdini mentioned back in 2013, I also ran into the error of subtracting giving a negative number, and excel just throws an error. No other response on the internet gave me good results. Although my method is longer, it always works with times in hh:mm:ss AM/PM. Just change the text format part of the formula for whatever you have. My data was:
11:15:45 PM 1:41:20 AM
I converted it to military time for ease with
=TEXT(A2, "hh:mm:ss")
Data was now:
23:15:45 01:41:20
I made another column, let's call it C, that just subtracts the second time from the first time,
=B2-A2
I next used an IF statement on this column, so that if the absolute value of the difference was the same as the number (a positive), I'd use
TEXT(B2-A2,"hh:mm:ss")
which just subtracts the later time from the earlier time. But...
If the absolute value of the difference was not the same, e.g. error-ville with a lot of hashes ##########, I use:
TEXT(24-(A10-B10),"hh:mm:ss")
Altogether, we have one more column, which finally spits out the correct hour difference. This column is predicated on columns A, B, and our error-prone difference column C:
=IF(C2=ABS(C2),TEXT(B2-A2,"hh:mm:ss"),TEXT(24-(A2-B2),"hh:mm:ss"))
this is the most ridiculous thing I have ever done. It's been years and such a simple case, never solved by Excel.

Round Excel Time Difference to Next 15 Minute Interval

I have a start time and an End Time in Excel:
Start Time
9:15 PM
End Time 9:30 PM
Time Spent 0:15
I'm looking to round up to the next 15 minute increment, and are using the formula:
=ROUNDUP(A1*96,0)/96
However, this rounds up the example data above from 0:15 to 0:30 (It should stay at 0:15 because it's a quarter hour already)
How to I avoid the rounding if the time difference is ALREADY on a quarter hour?
I would normally expect the original formula to work. When I test in Excel 2007 with manually input time values that are always whole minutes the original formula with ROUNDUP, brettdj's formula and the following formula:
=CEILING(A1,"0:15")
.....all give me the same results
....but if the times are derived from some sort of formula calculation then excel floating point arithmetic can cause very small rounding errors so that 0:15 is deemed to be very marginally above that value (although it would still display as 0:15:00.000) and is therefore rounded up to 0:30.
Because brettdj's suggestion only looks at hours and minutes that problem is avoided but it will round 0:15:59 to 0:15 too, so this approach might be preferable
=CEILING(MROUND(A1,"0:0:01"),"0:15")
That rounds the original time to the nearest second with MROUND (thus dealing with any floating point errors) and then rounds up to the next quarter hour
Edit: if you are using a formula to get the A1 time value it might be worth incorporating some rounding within that formula, e.g. wrap formula in MROUND like this to round to the nearest second
=MROUND(formula,"0:0:01")
It may look like that does nothing in terms of changing the formula results but that will potentially avoid any further floating point issues with any other calculations you might need to do.....and then your original formula with ROUNDUP should work......
You could use
=HOUR(A1)/24+CEILING(MINUTE(A1),15)/(24*60)
which converts a time in A1 of
- 9:15 PM to 9:15 PM
- 9:16 PM to 9:30 PM
etc

Resources