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.

Dani AI

Generated

Short answer: DateDiff with the "h" interval counts hour boundaries, it does not return the total hours component of a duration. That is why ’s point is correct — DateDiff("h", 12:30, 15:00) returns 3 (three hour boundaries crossed), while the true elapsed time is 2 hours 30 minutes. To get an exact H:MM:SS you must compute from the total seconds or use a TimeSpan and derive hours, minutes and seconds from that total.

A robust VB6/VBA-style approach (avoid ambiguous string parsing; pass Date values) is to get total seconds and split into components:

' VB6/VBA example
Function ExactElapsed(d1 As Date, d2 As Date) As String
    Dim totalSecs As Long, h As Long, m As Long, s As Long
    totalSecs = DateDiff("s", d1, d2)
    h = totalSecs \ 3600
    m = (totalSecs Mod 3600) \ 60
    s = totalSecs Mod 60
    ExactElapsed = h & ":" & Right("0" & m, 2) & ":" & Right("0" & s, 2)
End Function

In VB.NET use TimeSpan: Dim ts = endDt - startDt. ts.TotalHours gives fractional total hours; ts.Days*24 + ts.Hours or CInt(Math.Truncate(ts.TotalHours)) gives total whole hours while ts.Minutes and ts.Seconds give the remaining components.

Troubleshooting notes: do not use Hour()/Minute() on a duration — those return the time-of-day component (or TimeSpan.Hours = 0..23), not total hours. Always supply unambiguous dates (use DateSerial/ParseExact or ISO format) to avoid dd/mm vs mm/dd confusion. Also account for DST or timezone conversions if datetimes are local and may cross DST boundaries.

Recommended Answers

All 7 Replies

check out this code.

regards
Shouvik

thanks a lot :-)

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

I'm guessing but don't you mean 01/31 not 01/01?

Hello Tom,
The format is "dd/mm/yyyy"

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.