How to calculate time interval between 2 txt type fields?

N

Nosmo King

Hi Paul,

You can use the Mid function to pull the first two digits
and second two digits out of the string and then do the
math for hours and minutes. I tried using the DateDiff
function, but found that it rounds to the nearest whole
hour (eg. if they worked from 1600 to 1730 and got 1.5
hours, it would return "1" hour). You could use DateDiff
to calculate the number of minutes however. Maybe
something like this?

Dim strBeginTime As String
Dim strEndTime As String

Dim dtmBeginTime As Date
Dim dtmEndTime As Date

strBeginTime = 1600
strEndTime = 1745

'Convert to Dates
dtmBeginTime = CDate(Date & " " & TimeSerial(Mid
(strBeginTime, 1, 2), Mid(strBeginTime, 3, 2), 0))
dtmEndTime = CDate(Date & " " & TimeSerial(Mid(strEndTime,
1, 2), Mid(strEndTime, 3, 2), 0))


MsgBox "Time 1: " & dtmBeginTime & vbCrLf & "Time 2: " &
dtmEndTime & vbCrLf & vbrclf & "Minutes Between: " &
DateDiff("n", dtmBeginTime, dtmEndTime)

You might want to consider changing your field to a Date
datatype so you can just do a DateDiff on it right away,
but that may not be permissible in your situation.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top