• Status: Solved
• Priority: Medium
• Security: Public
• Views: 174

# 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.

0
bobubi
1 Solution

Commented:
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

0

Commented:
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
0

Author Commented:
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!
0

Commented:
bobubi  that will only return WHOLE hours, and will not correctly handle partial hour intervals....

0

Commented:
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
0

Author Commented:
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!
0

Author Commented:
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?
0

Commented:
the #....# cause VB to convert the STRING value between the #...#s to a Date value (as I described earlier).

If you have the values in a Date variable, then DON'T use the #, but if the values are in a textBox ( or as a string value), then USE the #s, or the cDate function....
0

Commented:
DateDiff gives the minutes also.

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

Author Commented:
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?
0

Commented:
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.
0

Commented:
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

0

## Featured Post

Tackle projects and never again get stuck behind a technical roadblock.