Wednesday, April 06, 2011

Rounding Waktu (contoh kasus dalam Ms Access)

Rounding dates and times

Note that the Date/Time data type in Access is a special kind of floating point type, where the fractional part represents the time of day. Consequently, Date/Time fields that have a time component are subject to floating point errors as well.

The function below rounds a date/time value to the specified number of seconds. For example, to round to the nearest half hour (30 * 60 seconds), use:
    =RoundTime([MyDateTimeField], 1800)

Public Function RoundTime(varTime As Variant, Optional ByVal lngSeconds As Long = 900&) As Variant
'Purpose: Round a date/time value to the nearest number of seconds
'Arguments: varTime = the date/time value
' lngSeconds = number of seconds to round to.
' e.g. 60 for nearest minute,
' 600 for nearest 10 minutes,
' 3600 for nearest hour,
' 86400 for nearest day.
'Return: Rounded date/time value, or Null if no date/time passed in.
'Note: lngSeconds must be between 1 and 86400.
' Default rounds is nearest 15 minutes.

Dim lngSecondsOffset As Long

RoundTime = Null 'Initialize to return Null.
If Not IsError(varTime) Then
If IsDate(varTime) Then
If (lngSeconds < 1&) Or (lngSeconds > 86400) Then
lngSeconds = 1&
End If
lngSecondsOffset = lngSeconds * CLng(DateDiff("s", #12:00:00 AM#, TimeValue(varTime)) / lngSeconds)
RoundTime = DateAdd("s", lngSecondsOffset, DateValue(varTime))
End If
End If
End Function

di tambil dari
Dikirim dari dengan dukungan dev-NET(system)