Link to home
Start Free TrialLog in
Avatar of bobubi
bobubi

asked on

Time Difference

Hi. Does anyone know any method to find the interval hours between two times?
For instance, 8:00AM - 9:00PM, how can I find the interval hours.

All answers are appreciated. Thanks
Avatar of Arthur_Wood
Arthur_Wood
Flag of United States of America image

dim Diff as Single

Diff = (EndTime - StartTime) * 24

Times/Dates are REALLY stored, internally, in VB, as NUMBERS, of type SINGLE (with a Decimal point), where the Integer part of the value(to the left of the decimal point) is the NUMBER of days since 12/31/1899, and the Fractional part(to the right of the decimal point)
is the fraction of 1 day, as measured in seconds.

AW

thus

Diff = (#9:00 PM# - #8:00 AM#) * 24

and this yields Diff = 13 (13 hours)

where

Dif = (#9:15 PM# - #8:00 AM#) * 24  ==> 13.25 (13 hours 15 minutes = 13.25 hours)

AW
Avatar of bobubi
bobubi

ASKER

I have just found out another solution:
DateDiff("h", Time1, Time2) = 13
This works too.

Anyway, I have to thank you AW for your help!
bobubi  that will only return WHOLE hours, and will not correctly handle partial hour intervals....

try it with Time1 = #9:15 PM# and Time2 = #8:00 AM#  to see what I mean...that will also return 13, but correctly should be 13 hours 15 minutes....

AW
Avatar of bobubi

ASKER

I have just found out another solution:
DateDiff("h", Time1, Time2) = 13
This works too.

Anyway, I have to thank you AW for your help!
Avatar of bobubi

ASKER

oh yah, I got what you mean.
It will produce a more exact time than using the DateDiff.
But may I know what's the two hex signs for?
ASKER CERTIFIED SOLUTION
Avatar of Arthur_Wood
Arthur_Wood
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
DateDiff gives the minutes also.

result = DateDiff("n", #8:15:00 AM#, #9:00:00 PM#)
hr = result \ 60
mn = result Mod 60
Avatar of bobubi

ASKER

by the way, does anyone know how to disable the checkboxes in the listview. I tried 'Ghosted' but only the items are frozen. I am still able to tick the checkboxes. Any solutions?
Hi bobubi,
This old question (QID 20561140) needs to be finalized -- accept an answer, split points, or get a refund.  Please see http://www.cityofangels.com/Experts/Closing.htm for information and options.
I have seached for code like the above. It is great. I am however having prolems getting times from a list box to to be inserted into the code. The time in the list is written "9:00 AM "

result = DateDiff("n", Time1  ,#12:00 PM#)

This resuls in the following error:
Type mismatch: 'Time1'

Help here would be much appreciated