Search: | |||||

| ||||

Home » Forum » MS Office | RSS Feeds |

I want to find the difference in hours between two days.

Like difference between 6/25/2007 12:30:00 and 6:28/2007 10:45:00....

I want to get the result in HH:MM format.

Dim intDiff as integer, strDiff as String

intDiff = DateDiff("n", date1, date2)

strDiff = CStr(Int(intDiff/60)) & ":" & CStr(intDiff mod 60)

Debug.Print strDiff

I believe you are wrong, and I assume you have not tried the code. I wrote

it into a function for you, and changed the integer to a long to handle

larger values.

Public Function HoursDiff(date1, date2) As String

Dim Diff As Long, strDiff As String

lngDiff = DateDiff("n", date1, date2)

strDiff = CStr(Int(lngDiff / 60)) & ":" & CStr(lngDiff Mod 60)

HoursDiff = strDiff

End Function

In my immediate window, I got:

?hoursdiff(#06/25/2005 6:20:00#, # 06/28/2006 13:30:00#)

8839:10

The key is the first argument to the DateDiff function -- the "n" computes

the number of minutes between two dates. You could check the docs on

DateDiff.

I believe I'm handing it to you on a silver platter. You might want to test

it.

I am sorry. It was my fault. I did test the code and it worked good.

Thanks for your help.

Didn't find what you were looking for?
Find more on To calculate the time difference between two days
Or get search suggestion and latest updates.

Related Topics:

- type casting /converting seconds to a days hours minutes seconds
- Time Difference for Page Creation time
- Calculating Time in parts of an hour
- find the number of working days (monday to firday) between two give
- calculate exeuction time
- help: Difference between two arrays
- Time difference calculations across midnight
- Finding difference in time
- return time in seconds in VBA?
- Time capture of less than one second
- Last record is repeated two times
- Write a class Time that has three data member hour,minutes and seconds.
- Display character which appears number of time equal half of maximum time
- when i press the menu->display time the time must updates every second.
- write a program that enters double data into an ArrayList and calculate the total
- Calculate Total Sum
- How to calculate IRR
- Excel won't calculate my function
- Easier method to capture & paste calculated value?
- Calculating the no. of containers
- Calculating duration
- Convert string like "=123+456+789" to Long and then calculate
- Application.Calculate fails
- Calculate fields (Pivot Tables)
- calculating 'names' of text boxes in a form