Hi mayurm38.
A date/time/datetime is a fractional number.
The whole number part is the day number where jan 1 1900 is day number 1.
The fractional part is the time. Since a day has 24 hours the value of 1 hour is 1/24 or 0.0416666667. 6 hours is 0.25.
So in order to calculate the time difference only, we have to get rid of the date part.
The next piece of code is one way of doing that.
-
Option Compare Database
-
Option Explicit
-
-
Sub timeDifference()
-
Dim dt As Date 'date and time
-
Dim tm As Date 'time only
-
Dim dif As Double
-
-
dt = #2/20/2017 2:30:00 PM#
-
tm = #8:30:00 AM#
-
-
dif = dt - Int(dt) - tm 'remove the date(whole number) part from dt
-
-
'next line prints
-
' 0,249999999997575 06:00:00
-
-
Debug.Print dif, Format(dif, "hh:mm:ss")
-
End Sub
-