If Statement to check time in excel - excel

I need to create an if statement to check time in excel. For instance if time is between
00:00:01 to 06:00:00 I give a value of 1
if time is 06:00:01 to 12:00:00 I give a value of 2
if time is 12:00:01 to 18:00:00 I give a value of 3
and if time is 18:00:01 to 00:00:00 I give a value of 4
Any ideas how I should implement this?

Assuming that the time is in cell A1, you could use:
=CEILING(A1/0.25,1)
It worked on some sample data for me:
12:01:03 3
18:19:00 4
00:00:01 1
The above works because dates are actually numbers in Excel, and 1 unit represents 1 day. So naturally, if you divide 1 day by 4, you get what you are looking for.

You can use Excel Worksheet formula like shown below (it reads the system time using function NOW()):
=IF(HOUR(NOW())>18,4,IF(HOUR(NOW())>12,3,IF(HOUR(NOW())>6,2,1)))
If HOUR is already given in, for e.g., column A1, then use a simplified formula:
=IF(A1>18,4,IF(A1>12,3,IF(A1>6,2,1)))

Try this
=TEXT(IF(INT(TEXT(A1,"[m]"))<=360,1,IF(INT(TEXT(A1,"[m]"))<=720,2,IF(INT(TEXT(A1,"[m]"))<=1080,3,IF(INT(TEXT(A1,"[m]"))<=1440,4,"")))),"#")

Related

How to calculate the hour between two hour times in Excel?

I have an excel sheet with the following in a row as an example:
'10:00 - 16:00'.. Typically it is HOUR - HOUR2.
I am trying to get the total hours based on this. So 10:00 - 16:00 would be 6 hours. 10:00 to 16:30 will probably be 6.5 hours etc.
Is there an possible way to do this in excel with a formula?
I'm ok with it not doing the overly complex ones (Ones with several hour - hour2 in one column) but it would make it easier if it at least did the ones that are simplistic.
Thanks!
With data in A1, in B1 enter:
=timevalue(RIGHT(A1,5))-timevalue(left(A1,5))
and then apply a time format to B1 to give hours:minutes. To get numeric hours, use:
=24*(TIMEVALUE(RIGHT(A1,5))-TIMEVALUE(LEFT(A1,5)))
You could try:
=TEXT(RIGHT(A1,(LEN(A1)-(FIND("-",A1)+1)))-LEFT(A1,(FIND("-",A1)-2)),"hh:mm:ss")
Results:

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

Excel Categorize Times of day

I am trying to create an excel function that categorizes the time of day.
I have a column of DateTimes, Format: 3/3/2017 13:30 (but can change the format if needed). I need a second column declaring "ON" for the hours 9 pm to 5 am, "AM" for 5 am to 9 am, "BH" for 9 am to 5 pm, and "PM" for the hours 5 pm to 9 pm. I need weekends too but I figure I can pull those manually.
Result would look like (where I have column 1 and need to calculate column 2):
DateTime Time period
3/3/2017 13:30 BH
3/3/2017 17:30 PM
3/4/2017 3:30 ON
3/5/2017 5:30 AM
Make a table with the lower time and expected out put:
Then a simple VLOOKUP:
=VLOOKUP(MOD(A2,1),F:G,2)
We can save a few characters by only testing one end of each range. Once we have tested a value for less than 5 am, we no longer need to test to see if it is greater than 5 am. (The AND( statements aren't necessary.)
=IF(HOUR(A1)<5,"ON",IF(HOUR(A1)<9,"AM",IF(HOUR(A1)<17,"BH",IF(HOUR(A1)<21,"PM","ON"))))
If you want to detect weekends, wrap the whole thing in a weekend test. Two methods presented:
The better approach, suggested by #ScottCraner in the comments:
=IF(weekday(a1,2)>5,"WEEKEND","THE WHOLE THING")
The first argument for weekday( is obviously the date we are testing. The second forces Monday to be the first day of the week, which leaves Sat & Sun as the last two.
Combined with the rest, we would get:
=IF(weekday(a1,2)>5,"WEEKEND",IF(HOUR(A1)<5,"ON",IF(HOUR(A1)<9,"AM",IF(HOUR(A1)<17,"BH",IF(HOUR(A1)<21,"PM","ON")))))
An unnecessarily long, and somewhat fragile approach (breaks in non-English)
=IF(LEFT(TEXT(A1,"ddd"),1)="S","WEEKEND","THE WHOLE THING")
This works because TEXT(A1,"ddd") formats the date as the three-letter day of week. In English, at least, both weekend days starts with an "S", and we use left( to grab that first letter.
Together, it would end up looking like:
=IF(LEFT(TEXT(A1,"ddd"),1)="S","WEEKEND",IF(HOUR(A1)<5,"ON",IF(HOUR(A1)<9,"AM",IF(HOUR(A1)<17,"BH",IF(HOUR(A1)<21,"PM","ON")))))
Just to show a different way:
=IF(NETWORKDAYS(A2,A2),CHOOSE(SUM((({"5:00";"9:00";"17:00";"21:00"}*1)<MOD(A2,1))*1)+1,"ON","AM","BH","PM","ON"),"WEEKEND")
This is an array formula and must be confirmed with ctrl+shift+enter.
Can be extended as you like without getting in trouble of the bracket-limit.
Also like the Scott's answer: this works with times like 17:43. ;)
A little long, but works:
=IF(AND(HOUR(A2)>=9,HOUR(A2)<17),"BH",IF(AND(HOUR(A2)>=17,HOUR(A2)<21),"PM",IF(AND(HOUR(A2)>=5,HOUR(A2)<9),"AM","ON")))

Set 00:00 as the last timestamp in Excel

I have a feed of 30 minutes meter readings from MySQL to Excel sorted by the first column which is a datetime format, i.e 10-06-2015 00:00:00, 10-06-2015 00:30:00, 10-06-2015 01:00:00 etc.
Because the reading from 00:00:00 each day is actually data from the last 30 minutes of the day, I need the 00:00:00 reading to be the last row for the day rather than the first (as Excel is sorting by time order 00:00 is the default start of each day). Does anyone know how to format this in Excel so that the 10-06-2015 00:00:00 will come after the 10-06-2015 11:30:00 row and not after the 09-06-2015 11:30:00 row?
I don't think you are going to find a way to do that using just formats. Reason being that the format doesn't change the underlying value, which is what excel uses to do the sort in the first place.
I would suggest using a helper column in which you use a formula... maybe something like this:
=IF(A2-ROUNDDOWN(A2,0)=0,A2+0.99999,A2)
Where the first value of your time series is in A1... then copy the formula down and sort on the column B. This worked for me.

Subtract two times of day to get interval length in hours

I am working on creating Time sheet in excel and i have the format like this.
A B C D E F
Date Day Time IN Time OUT Lunch Total
8/29/2011 Monday 9.00 18.00 0.45 8.55
I am not able to get correct hours in Total field. Correct value should have been 8.15 and not 8.55.
This is the current formula i am using in to calculate Total.
=D2-C2-E2
Can someone please help me get the formula right?
Instead of typing e.g. 9.00 in your cells, type 9:00 (and format your cells as time to make sure they display correctly).
The formula itself is fine, but currently you're just subtracting decimal numbers, and of course the correct result is 8.55.

Resources