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