Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

time diff function

Status
Not open for further replies.

riffraff

Mechanical
Oct 9, 2003
1
Does anyone know how I can determine the number of hours between 2 different times that are entered in the below format? I want to populate another cell with the actual number of hours.

11a/6p

Thanks in advance for any suggestions!
 
Replies continue below

Recommended for you

Are you trying to calculate 11 am divided by 6 pm? I don't think that has any meaning.

On the other hand, if you're trying to compute the difference between 11 am and 6 pm, as in started work at 11, ended at 6, that's seven hours, you might try this:

1. Format the cells in question, including the calculation cells as "time" cells - picking your favorite, i.e., military, etc. Or make a custom time format so 11:00 shows up as "11a" - it's easy.

2. After that, it's pretty straight-forward. The only trick is that you _have_ to enter the hours and minutes as "11:00" and not "11" or "11a" as Excel won't know what you mean.

3. I've found that using the military time format makes things easier (e.g., 6 pm = 18:00, etc.).

4. Excel will do the math properly, that is, 6 pm - 11 am = 7 hours, or 7:00. Play around with the Format > Cell capabilities, you'll find what you want.
 
Assuming that you will get the timeformat because that you import from some other program that will only give this format i made a small macro that will do the job BUT ONLY FOR WHOLE HOURS!

Public Function ellapsed_time(start, finish)
If Right(start, 1) = "a" Then
start = Left(start, Len(start) - 1)
Else
start = Left(start, Len(start) - 1) + 12
End If
If Right(finish, 1) = "a" Then
finish = Left(finish, Len(finish) - 1)
Else
finish = Left(finish, Len(finish) - 1) + 12
End If
ellapsed_time = finish - start
End Function

You can copy paste from your browser and right into the EXCEL VB editor

Just call the function and the result will be the no. of hours between start and end.

Best Regards Morten
 
And one small item more: The function will not be correct if your start and finish is before and after midnight.

Best Regards

Morten
 
Format the cells like stated by DaveViking
Lets say you have the times in C1 and B1

use the functions HOUR and MINUTE

if C1 = 21:23 and B1 = 14:24

=HOUR(C1-B1) for the difference in hours = 6
=MINUTE(C1-B1) for the difference in minutes = 59

Regards

Steven van Els
SAvanEls@cq-link.sr
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor