D
Douglas J. Steele
The Date/Time data type in Access is really only meant for point-in-time
(timestamps), not for durations. Date/Times are actually 8 byte floating
point numbers, where the integer part represents the date as the number of
days relative to 30 Dec, 1899, and the decimal part represents the time as a
fraction of a day. In other words, if you have 1:30, Access actually stores
0.0625 (and treats that as 01:30:00 on 30 Dec, 1899) You can do the
arithmetic, but as soon as the sum exceeds 1, Access assumes that's a day
portion, and only shows the remainder as time.
The best way is to store your durations as something else. Depending on the
resolution you want, store them as minutes or even seconds, do the
arithmetic on that, then have a custom function that will convert to how you
want to display, such as
Function FormatTimeInSeconds(TimeInSeconds As Long) As String
Dim lngDays As Long
Dim lngHours As Long
Dim lngMinutes As Long
Dim lngSeconds As Long
Dim lngSecondsRemaining As Long
Dim strFormattedTime As String
' 86400 is the number of seconds in a day: 24 * 60 * 60
lngDays = TimeInSeconds \ 86400
lngSecondsRemaining = TimeInSeconds - (lngDays * 86400)
lngHours = lngSecondsRemaining \ 3600
lngSecondsRemaining = lngSecondsRemaining - (lngHours * 3600)
lngMinutes = lngSecondsRemaining \ 60
lngSeconds = lngSecondsRemaining - (lngMinutes * 60)
Select Case lngDays
Case 0
strFormattedTime = vbNullString
Case 1
strFormattedTime = "1 day "
Case Else
strFormattedTime = Format$(lngDays, "0") & "days "
End Select
FormatTimeInSeconds = strFormattedTime & Format$(lngHours, "00") & _
":" & Format$(lngMinutes, "00") & ":" & _
Format$(lngSeconds, "00")
End Function
(timestamps), not for durations. Date/Times are actually 8 byte floating
point numbers, where the integer part represents the date as the number of
days relative to 30 Dec, 1899, and the decimal part represents the time as a
fraction of a day. In other words, if you have 1:30, Access actually stores
0.0625 (and treats that as 01:30:00 on 30 Dec, 1899) You can do the
arithmetic, but as soon as the sum exceeds 1, Access assumes that's a day
portion, and only shows the remainder as time.
The best way is to store your durations as something else. Depending on the
resolution you want, store them as minutes or even seconds, do the
arithmetic on that, then have a custom function that will convert to how you
want to display, such as
Function FormatTimeInSeconds(TimeInSeconds As Long) As String
Dim lngDays As Long
Dim lngHours As Long
Dim lngMinutes As Long
Dim lngSeconds As Long
Dim lngSecondsRemaining As Long
Dim strFormattedTime As String
' 86400 is the number of seconds in a day: 24 * 60 * 60
lngDays = TimeInSeconds \ 86400
lngSecondsRemaining = TimeInSeconds - (lngDays * 86400)
lngHours = lngSecondsRemaining \ 3600
lngSecondsRemaining = lngSecondsRemaining - (lngHours * 3600)
lngMinutes = lngSecondsRemaining \ 60
lngSeconds = lngSecondsRemaining - (lngMinutes * 60)
Select Case lngDays
Case 0
strFormattedTime = vbNullString
Case 1
strFormattedTime = "1 day "
Case Else
strFormattedTime = Format$(lngDays, "0") & "days "
End Select
FormatTimeInSeconds = strFormattedTime & Format$(lngHours, "00") & _
":" & Format$(lngMinutes, "00") & ":" & _
Format$(lngSeconds, "00")
End Function