Calculate time difference in excel - excel

I have a two dates in excel "1/2/2016 01:56:05" and "8/3/2016 06:21:46". How do I calculate the time difference between them in a format of days and hours?
Thanks!

Try this to include the spelled out hours and minutes:
=INT(A2-A1) & " days, " & TEXT(A2-A1,"h "" hours, ""m "" minutes""")
Note that since the m comes immediately (ignoring the separator text in quotes) after the h it will be interpreted as minutes and not months

I prefer to work with dates as decimals. It looks cumbersome, but I'm much happier knowing what it's doing.
What I would usually do is to have A1-A2 in cell A3, then work out the component parts separately:
Days: =INT(A3)
Hours: =INT((A3-INT(A3))*24)
Minutes: =INT(((A3*24)-INT(A3*24))*60)
Or if you wanted to do it all in one formula:
=INT(A3)&" days, "&INT((A3-INT(A3))*24)&" hours, "&INT(((A3*24)-INT(A3*24))*60)&" min"
or without using A3
=INT(A1-A2)&" days, "&INT(((A1-A2)-INT(A1-A2))*24)&" hours, "&INT((((A1-A2)*24)-INT((A1-A2)*24))*60)&" min"
It's not the prettiest or most efficient method but you can see how the times are calculated this way.

Assuming the dates are in cells A1 and A2, this will produce an answer with minutes as well in d:hh:mm format.
=INT(A2-A1) & ":" & TEXT(A2-A1,"hh:mm")
Drop the :mm if you don't need minutes.
If you want text:
=INT(A2-A1) & " days, " & TEXT(A2-A1,"h") & " hours"
If you want text with minutes:
=INT(A2-A1) & " days, " & TEXT(A2-A1,"h"" hours, ""m"" minutes""")
Using double quotes alongside each other "escapes" the quote itself and allows the extra text to appear in the string. As Ron stated in his answer, the m following an h in the same format string indicates minutes, so you can save an extra A2-A1 calculation by putting both in a single format.

Related

Is there an excel function that I can use to give values based on what the time value is?

I'm looking for a function I can use that returns "Night Off Prime" if the time a programme was on was between 11pm - 5:59am, "Day Off Prime" if it was on between 6am - 5:59pm and "Prime" if it was on between 6pm - 10:59pm.
I've tried using the IFS function with the code being =IFS(OR(G3>=23,G3<6),"Night Off Prime", OR(G3>=6,G3<18),"Day Off Prime", OR(G3>=18,G3,23),"Prime")
The G column is just the hh format of the hour it was on, with the values formatted as Number.
I've also tried =IFS(OR(BK3>=23:00,BK3<06:00),"Night Off Prime", OR(BK3>=06:00,G3<18:00),"Day Off Prime", OR(BK3>=18:00,BK3,23:00),"Prime")
Here the BK column is the time the programme was on in the hh:mm time format
The main trouble I'm finding there is how to label time between two different times such as between 11pm - 5:59am instead of just past 11pm. I used the OR logical but that doesn't seem to have worked.
I've also tried a VLOOKUP function =VLOOKUP(BK2,$AC$899:$AD$901,2, TRUE)
Here the table array is
AC899 Night Off Prime AD899 23:00-05:59
AC900 Day Off Prime AD900 06:00-17:59
AC901 Prime AD901 18:00-22:59
If you prefer the IFS you may want to adjust like this (should work in the similar way for hh:mm as well):
=IFS(G3<6;"Night Off Prime";G3<18;"Day Off Prime";G3<23;"Prime";TRUE;"Night Off Prime")
(may replace ; with , depending on regional setting)
alternatively:
=IFS(G3<0;"#N/A";G3<6;"Night Off Prime";G3<18;"Day Off Prime";G3<23;"Prime";G3<24;"Night Off Prime";TRUE;"#N/A")
"The main trouble I'm finding there is how to label time between two different times such as between 11pm - 5:59am instead of just past 11pm."
Add a column H2 = G2 + 1/24
Then your ranges are 0 to 7/24, 7/24 to 19/24 and 19/24 to 24/24, so you don't have an interval split over a resetting of the clock to zero.

Using Unix US Formatted Date-time-year for calculations in Excel

I have dates that are formatting using the default time formats for Bash (echo $date) for example:
Fri Dec 24 07:35:41 EST 2021
I cannot change this behavior as I don't have permissions to alter our IT solution. I need to be able to load these dates into a UK localised Excel, but any attempts to use the usual Text To Columns DMY approach doesnt work due to the Day, time zone, time etc.
Is the only way to extract the data and re-assemble it or is there a simpler solution?
Try this function . write it to module and call from any cell . Like =makeDateFromStr(A1)
Function makeDateFromStr(ByVal someDate As String) As Date
On Error Resume Next
Dim var As Variant, Mu As Long
var = Split(someDate, " ") ' array to hold all parts of the string-date
Mu = (InStr("JanFebMarAprMayJunJulAugSepOctNovDec", var(1)) + 2) / 3 ' a way to make short string month (Dec) to number
makeDateFromStr = DateSerial(var(5), Mu, var(2)) & " " & TimeValue(var(3)) ' putting all together
On Error GoTo 0
End Function
As far as I can see, if we get rid of the EST and the week day FRI, we can get a valid date value. Note: If you want the weekday, you can use the function WEEKDAY() to get the value 6, which is Friday. (1 is Sunday).
So, to get rid of the weekday, you can use the RIGHT() function.
All weekdays are 3 letters and a space, so we need to get rid of the 4 first characters, like so:
=RIGHT(A1;LEN(A1)-4)
You can put this just underneath your example time, so place this in cell A2.
To remove the EST is a bit more tricky, we need to know where in the text EST is. For this, we use FIND():
=FIND("EST ";A2)
Now we know at which position the text "EST " appears in A2.
Then we can use a simple REPLACE() function, to replace "EST " with nothing. Like so:
=REPLACE(A2;FIND("EST ";A2);4;"")
Note: The 4 refers to the length of "EST ".
This worked for this example, so you can try it out with different data. Just comment below if it isn't working correctly, and with what data and I'll look into it.
Put a date in A1
Put =RIGHT(A1;LEN(A1)-4) in A2
Put =REPLACE(A2;FIND("EST ";A2);4;"") in A3
You could also merge it into 1 cell:
=REPLACE(RIGHT(A1;LEN(A1)-4);FIND("EST ";RIGHT(A1;LEN(A1)-4));4;"")
You could then place this next to the date in A1, so in B1, and drag this formula down for ALL your imported dates. Now it should work for all of your dates.
You could also copy their values and replace the original dates, then remove the formulas, and you will have the same layout and functionality you were expecting from the beginning!

Find the difference of days between 2 timestamps in Excel

I have 2 columns, date opened and date closed which have timestamps for an excel sheet with thousands of rows.
A B
1 Date Opened Date Closed
2 07/16/2019 04:19 PM 07/24/2019 11:39 AM
Issue:
If I try Days360(A2,B2) I get ####### as output. On taking cursor over the cell of Days360(A2,B2) I get message, days too large value or negative hence shown as ####
Same problem of ###### with Datedif.
=month(B2) & '/' day(B2) & '/' & year(B2) also does not work
Is there any way to calculate number of days between 2 timestamps?
=MONTH(B2) & "/" & DAY(B2) & "/" & YEAR(B2)
This works. Apparently you need double quotes and not single quote around /.

I have a time in a cell, want to minus 2 hrs and add 2hrs to the time and display in excel

I have a time in a cell, want to minus 2 hrs and add 2hrs to the time and display in excel
Example : 02/08/2020 11:00AM
So Minus 2hrs and plus 2 hrs next cell must display as 9:00AM-13:00PM
I tried the below formula but displays like 43811.0577893518 - 43811.2244560185
=CONCATENATE(A1-TIME(2,0,0)," ","-"," ",A1+TIME(2,0,0))
Subtract & add two hours (2/24) to your date
=TEXT(A1-(2/24),"hh:mm AM/PM") & " - " & TEXT(A1+(2/24), "hh:mm AM/PM")
Use TEXT to format the output:
=CONCATENATE(TEXT(A1-TIME(2,0,0),"HH:MM AM/PM")," ","-"," ",TEXT(A1+TIME(2,0,0),"HH:MM AM/PM"))

Excel formula to SUBSTITUTE value with specific parameters

I have formula to convert text "2 hours 43 mins" to "2,43". Sometimes value is "1 hour 43 mins" and my current solution does not work in this case. How to make it work in both cases "2 hours 43 mins" and "1 hour 43 mins"? I have tried to use OR with no success.
This is my current formula:
=SUBSTITUTE(SUBSTITUTE(Q68;"mins";"");" hours ";",")
I have tried:
=SUBSTITUTE(SUBSTITUTE(Q68;"mins";"");OR(" hours ";" hour ");",")
Try this:
=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(Q68;"s";"");"min";"");" hour ";",")
How about:
=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(Q68;"min";"");" hour";",");"s";"")
EDIT#1:
The "best approach":
replace "hour"
replace "min"
replace "s"
replace " "

Resources