SUBSTITUTE formula to separate hours and minute by comma - excel

I have formula to separate hours and minutes by comma.
=CEILING(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(P96;"s";"");" min";"");" hour ";",");0,1)
This works for 4 hours 40 mins and 1 hour 10 mins. However there might case like 20 mins. Then my formula is displaying 20 that is not correct and should be 0,2. Any suggestions how to fix this issue?
Note! Values like 4 hours 40 mins are in one cell, in this case in cell P96. Information comes straight from Google Maps xml.

=CEILING(IF(ISNUMBER(SEARCH("hour";P96));SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(P96;"s";"");" min";"");" hour ";",");"0,"&SUBSTITUTE(SUBSTITUTE(P96;"s";"");" min";""));0,1)

Conditionally add a dummy prefix that satisfies the hours substitution.
=CEILING(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(IF(ISERROR(SEARCH("hour"; P96)); "0 hours "; "")&P96;"s";"");" min";"");" hour ";",");0,1)

Type your time in cell A1. As an example, you could type "2:30", "14:30" or "2:30 PM".
Type "=HOUR(A1)" in cell A2 to produce only the hour. As an example, extracting the hour "2:30 PM" would give you "14," because 2 PM is the fourteenth hour in the day.
Type "=MINUTE(A1)" in cell A3 to extract the minutes.
Type "=SECOND(A1)" in cell A4 to extract the seconds.

Related

Custom time format excel: don't display zeros if hours or minutes are zeroes

There was an question placed before and it got the answer:
Thanks for that.
"Right now I have formatted my cell with:
h "hours" m "minutes"
So if my cell has 7:00, it displays as 7 hours 0 minutes. Is there a way to remove the hours or the minutes if these are zero? Something like 7 hours, or 0:30 as 30 minutes"
The answer was:
If you have Excel 2007 or later versions (but apparently not Excel for Mac 2011) you can achieve this with a combination of regular formatting and conditional formatting.
Assuming data in A1 down use a regular custom format like this
[<0.0415][m]" minutes";h "hours" m "minutes"
That will give you the same as your previous formatting except that any value < 01:00 will display like
30 minutes
Now you can add a conditional formatting condition. With column A selected do this
Conditional Formatting > New Rule > Use a formula to determine which cells to format > type this formula in the box
=MINUTE(A1)=0
Click the "format" button and set the following number format
h" hours"
Now if you have 16:00 in A1 that will display as just 16 hours
[conditional formatting rules will always supersede regular formatting]
But I wonder if it is possible in addition to this add time format in a way like if I have 1 hour it shows "1 hour" (in singular) instead of "1 hours"?
This should do this trick
Add this to Custom type box
[<0.0415][m] "minutes";[<0.083][h] "hour" m "minutes";[h] "hours" m "minutes"
inputs/outputs
inputs -
00:05:00
00:30:00
01:05:00
01:30:00
02:30:00
outputs -
5 minutes
30 minutes
1 hour 5 minutes
1 hour 30 minutes
2 hours 30 minutes
Notes
By putting the square bracket around [h] it will allow for more than 24 hours to be added e.g. 31 hours 30 minutes.
In Excel 1 minute is equal to 0.00069444 (1/1440 of a day). Knowing this helps to calculate the two less-then numbers.
Alternative option
If you just want h rather than hour and m rather than minutes try this
[<0.0415][mm] "m";[h] "h" mm "m"
=IF(HOUR(B9)<>0,HOUR(B9)&" hour"&IF(HOUR(B9)<>1,"s "," "),"")&IF(MINUTE(B9)<>0,MINUTE(B9)&" minute"&IF(MINUTE(B9)<>1,"s",""),"")
TLDR : extract every values manually > then put if conditions for 0,1 and other > print value..
Hope it helps.

formating numbers into Time format

I want to convert numbers (that represent seconds) into HH:MM:SS format (like shown in pic..) but don't want to split them.. I want to format it using some formula or
If you just want to display the 715 seconds as hours:minutes:seconds, you will need a helper column that you use just for display.
You can create it either as in your example screenshot, or using a formula:
=TEXT(B2/86400,"[hh]:mm:ss")
Note the brackets around hh. That keeps the hours from rolling over every 24 and displays total hours correctly for instances where seconds > 86,400.
There's no way I know of to have 715 displayed as the associated hours:minutes:seconds in the same cell.
You can use a formula like this to convert 715 to HH:MM:SS:
=TEXT(FLOOR((A1-FLOOR(A1/86400,1)*86400)/3600, 1), "00:") & text(floor((A1 - floor(A1/3600,1)*3600)/60, 1),"00:") & text(A1 - floor(A1/3600,1)*3600 - floor((A1 - floor(A1/3600,1)*3600)/60, 1)*60,"00")
Let's break it down.
Get hours
A1 is seconds
There are 86400 seconds in a day
FLOOR(A1/86400, 1) gives us the floor of days
Multiply that by 86400 will give us seconds in those days FLOOR(A1/86400,1)*86400
Remove those many seconds from A1 and you get seconds remaining in the fraction of the last day A1- FLOOR(A1/86400,1)*86400
Divide those remaining seconds with 3600 to get hours FLOOR((A1- FLOOR(A1/86400,1)*86400 )/3600, 1)
Use TEXT function to format it to 2 digits and a colon at the end TEXT(FLOOR((A1- FLOOR(A1/86400,1)*86400 )/3600, 1), "00:")
Then, find remaining minutes and format it.
Then, take remaining seconds and format it.
Alternate method
Assume that your seconds are in column A6.
In B6, we will put Whole Days =FLOOR(A6/86400, 1)
In C6, we will put remaining seconds =A6-B6*86400
In D6, we will put Whole Hours =FLOOR(C6/3600,1)
In E6, we will put remaining seconds =C6-D6*3600
In F6, we will put Whole Minutes =FLOOR(E6/60, 1)
In G6, we will put remaining seconds =E6-F6*60
In H6, we'll format Hours, Minutes and Seconds =TEXT(D6,"00:") & TEXT(F6,"00:") & TEXT(G6,"00")

How do I calculate this time? Across midnight

I have a sheet with start and end dates and values.
In C I have start date.
In E I have end date.
In I I have start time. (06:05:00)
In J I have end time. (08:33:00)
If C <>E I need to add 24 hours to the time elapsed.
How can I do that?
I tried if(C2<>E2;1+J2-I2;"omitted")
But I get the result 21:32:00.
It should be about 26 hours
(24+8-6 = 26 if we only look at the hours).
What have I done wrong?
Edit;
Back at work and can now upload some images.
Method 1
Method 2
Both return the wrong time.
EDIT2;
Method 3
I remember how I always have to format the dates from "our" format to Excels format for it to be recognized as a date.
In column P I use RIGHT(), MID(), and LEFT() to make a correct formatted date.
In R and S I use the same as P&Q column.
Still not correct.
:-/
EDIT again:
Use formula: =(E1+J1)-(C1+I1) we just add the date and time together and subtract the end from start and then format the cell having the formula to show days, hours, minutes.
This way, if you have more than 1 day difference, you're not just adding 24 hours.
Change the format of the target cell contianing the above formula to
d "days" hh "hours" mm "minutes"
or use the format tigerAvatar suggested of [hh]:mm if you want the hours to be cumulative across days.
Then you get a nice output of: 2 days 02 hours 28 minutes or 50:28
Feel free to drop the 1 h or 1 m if you don't want the leading zeros.
A picture is worth a 1000 words so:
Version 2 after your screenshots: I don't think C, E are in date format based on your updates so...
I used formula=(DATE(LEFT(E2,4),MID(E2,5,2),RIGHT(E2,2))+J2)-(DATE(LEFT(C2,4),MID(C2,5,2),RIGHT(C2,2))+I2) in K and custom format mask: [h]:mm
If this doesn't work it may be a regional setting and the interpretation of [h]:mm I am assuming I/J are time formats.

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.

Rounding to two specific times

I need a formula that will round to either a time 15 minutes or 45 minutes past the hour based on the minutes in a time..
Example: If I have a time in A1 and the minutes are 46 and 14, I want the formula to return the time of 15 past the hour.
If A1 has 11:47 then return 12:15.
If I have a time in A1 and the minutes are between 16 and 44, I want the formula to return the time of 45 past the hour.
If A1 has 12:23 then return a time of 12:45
Any time that is either 15 or 45 past the hour will not change.
=CEILING(A1+TIME(0,15,0),TIME(0,30,0))-TIME(0,15,0)
Add 15 minutes
Roundup to the nearest 30 minutes
Subtract 15 minutes
This is a bit complicated but... (updated)
=(CEILING((A1-(15/(24*60)))/(1/48), 1)*(1/48))+(15/(24*60))
It goes something like this:
Subtract 15m to the time
Divide by 30m
Round up this value
Multiply by 30m
Add 15m
Here is a formula using the logic from my comment above:
=IF(AND(A1>=0,A1<=15),15,(IF(AND(A1>15,A1<=45),45,15)))
Remember, you will also need to add 1 to the hour in the final case (where A1 > 45).
Here is a breakdown of the above formula:
1. If A1 is between 0-15, then it will return 15.
2. If A1 is between 16-45, then it will return 45.
3. If A1 is between 46-59, then it will return 15. (the logic to add an hour is not handled in this formula)

Resources