cdate(cdbl((ItemDateCreated1)+cdbl(ItemDateCreated2))/2)
NewDate = CDate((CDbl(ItemDateCreated1) + CDbl(ItemDateCreated2)) / 2)
NewDate = CDate((ItemDateCreated1 + ItemDateCreated2) / 2)
' or
NewDate = CDate(ItemDateCreated1 / 2 + ItemDateCreated2 / 2)
Option Explicit
Public Sub Test()
Const NUMBER_FORMAT As String = "0.0000000000"
Dim count As Long
Dim doubleTime As Double
Dim dateTime As Date
Dim tenthSecond As Double
dateTime = CDate(0)
tenthSecond = CDbl(DateAdd("s", 1, CDate(0))) / 10#
Debug.Print "tenthSecond:", Format(tenthSecond, NUMBER_FORMAT)
For count = 0 To 9
doubleTime = doubleTime + tenthSecond
Debug.Print Format(doubleTime, NUMBER_FORMAT), Format(CDbl(CDate(doubleTime)), NUMBER_FORMAT), Format(CDate(doubleTime), "hh:mm:ss")
Next count
End Sub
What is the format in a query grid that will show milliseconds?You need a custom format function. E.g.
Option Explicit
Private Type TypeSystemTime
Year As Integer
Month As Integer
DayOfWeek As Integer
Day As Integer
Hour As Integer
Minute As Integer
Second As Integer
Millisecond As Integer
End Type
Private Declare Sub GetSystemTime Lib "kernel32" (lpSystemTime As TypeSystemTime)
Public Sub Test()
Dim TimeAsString As String
Dim TimeAsDate As Date
TimeToMillisecond TimeAsString, TimeAsDate
Debug.Print TimeAsString, TimeAsDate, FormatWithMilliseconds(TimeAsDate)
End Sub
Public Function FormatWithMilliseconds(ByVal CValue As Variant, Optional ByVal CBaseFormat As String = "hh:nn:ss") As String
On Local Error GoTo LocalError
Dim Result As String
Dim TimeReminder As Double
Dim ValueAsDate As Date
Result = "#N/A"
If Len(Trim(CValue & "")) > 0 Then
ValueAsDate = CDate(CValue)
TimeReminder = CDbl(ValueAsDate) - CLng(ValueAsDate) - CDbl(TimeSerial(Hour(ValueAsDate), Minute(ValueAsDate), Second(ValueAsDate)))
TimeReminder = TimeReminder * 86000000#
Result = Format(ValueAsDate, CBaseFormat) & "." & Format(TimeReminder, "0000")
End If
LocalError:
FormatWithMilliseconds = Result
End Function
Public Sub TimeToMillisecond(ByRef OTimeAsString As String, ByRef OTimeAsDate As Date)
Dim SystemTime As TypeSystemTime
GetSystemTime SystemTime
OTimeAsString = _
Format(SystemTime.Hour, "00") & ":" & _
Format(SystemTime.Minute, "00") & ":" & _
Format(SystemTime.Second, "00") & "." & _
Format(SystemTime.Millisecond, "0000")
OTimeAsDate = CDate(CDbl(TimeSerial(SystemTime.Hour, SystemTime.Minute, SystemTime.Second)) + CDbl(SystemTime.Millisecond) / 86000000#)
End Sub
This shows, that the fractions are used, but you may already run into floating point issues.