Hello , need some help on following code:

HH = DateTime.DateDiff("h", "01/01/2008 12:30:00", "01/01/2008 15:00:00")
MM = DateTime.DateDiff("n", "01/01/2008 12:30:00", "01/01/2008 15:00:00")
SS = DateTime.DateDiff("s", "01/01/2008 12:30:00", "01/01/2008 15:00:00")

MsgBox HH & ":" & MM Mod 60 & ":" & SS Mod 60

the result shows : 3:30:0

im expecting result like 2:30:0

ur advice is appreciated.

Interesting time math error... ignoring the modulo half hour when subtracting the hours. Confusion sets in with time math when we switch bases (base 60 for minutes and seconds, base 24 for hours) and pretend there is still positional value which is only true for identical bases, like money, which is all base 10.

Years of interfacing with timeclocks makes it old hat for me but everyone runs into the same problems.

Try always asking yourself what exactly is this function defined to return for these parameters?

The answer to the "h" is hours and so 15 - 12 = 3.

Need some advice for this code:

Dim Text1 As Date
Dim Text2 As Date
Text1 = "01/01/2008 12:30:00"
Text2 = "02/01/2008 15:00:00"
diff = Text2 - Text1
MsgBox "Difference between two time : " & TimeValue(Text2) - TimeValue(Text1) & Chr(13) & _
"Hour Difference :" & Hour(diff) & Chr(13) & _
"Minute Difference :" & Minute(diff) & Chr(13) & _
"Second Difference :" & Second(diff), , "TimeValue Comparison"

its giving me 2:30:00
actually it should be 26:30:00

Any updates on this?

i need a general function which can give me exact difference for times. The function will take 2 parameters (date1time1, date2time2) and return the exact time difference.

using datediff("h"....) datediff("n"....) datediff("s"....) is not working as shown in example below:
rdatetime="01/01/2008 15:45:00"
repdatetime="01/01/2008 16:30:00"
HH = DateTime.DateDiff("h", rdatetime, repdatetime)
MM = DateTime.DateDiff("n", rdatetime, repdatetime)
SS = DateTime.DateDiff("s", rdatetime, repdatetime)

This is returning 1 for hours which is not expected.

The example attached in prevous reply to this thread is working only for 24 hours... if i have different dates its showing me same result.

This article has been dead for over six months. Start a new discussion instead.