我正在创建一个VB6应用程序,该应用程序需要查询现有数据库,并且需要按日期进行筛选。数据库以Windows文件时间格式存储日期,即130159451238571322
,它是2013-06-17 12:18:43.857
VB6中是否有方法获取当前日期/时间并将其转换为Windows文件时间?
经过一点研究,我似乎没有任何本机函数或过程可以为您做到这一点(在VB6中,也就是说,VB.NET允许您做DateTime.Today.ToFileTime
)。然而,本文解释了Windows文件时间的计算方法-文件时间
考虑到这一点,您可以计算1601年1月1日午夜到当前日期和时间(UTC)之间发生的100纳秒间隔的数量,以创建准确的文件时间。
VB6本机处理的64位值只有Currency类型的值。然而,这些是带符号的64位整数,隐含缩放10000,因此出于算术目的,二进制值1被视为0.0001。
然而,当您通过ADO从数据库中检索这样的"文件时间"值时,您得到的通常是Decimal子类型的Variant。这完全是另一种生物。
FILETIME是一个结构,在VB6中最接近的是一个更恰当地称为记录的"UDT"。因此,当在数据库中使用这些值时,您可以玩一个更轻率的失败游戏。
下面是一些转换的例子:
Option Explicit
Private Type OneCurrency
Curr As Currency
End Type
Private Sub cmdConvert_Click()
Dim DateTime As Date
Dim FileTimeRecord As FILETIME
Dim DecFileTime As Variant
Dim OC As OneCurrency
'Convert:
DateTime = CDate(txtTime.Text)
FileTimeRecord = DateToFileTime(DateTime)
LSet OC = FileTimeRecord
DecFileTime = CDec(OC.Curr) * CDec(10000)
'Display:
With FileTimeRecord
lblFileTime.Caption = Right$(String$(7, "0") & Hex$(.dwHighDateTime), 8) _
& ":" _
& Right$(String$(7, "0") & Hex$(.dwLowDateTime), 8) _
& vbNewLine _
& CStr(DecFileTime)
End With
'Convert back, display:
OC.Curr = DecFileTime / 10000@
LSet FileTimeRecord = OC
lblTime.Caption = CStr(FileTimeToDate(FileTimeRecord))
End Sub
Private Sub cmdNow_Click()
txtTime.Text = CStr(Now())
End Sub
该Form代码依赖于位于其他位置的一些声明,例如静态模块:
Option Explicit
Public Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Private Type SYSTEMTIME
wYear As Integer
wMonth As Integer
wDayOfWeek As Integer
wDay As Integer
wHour As Integer
wMinute As Integer
wSecond As Integer
wMilliseconds As Integer
End Type
Private Declare Function FileTimeToSystemTime Lib "kernel32" ( _
ByRef lpFileTime As FILETIME, _
ByRef lpSystemTime As SYSTEMTIME) As Long
Private Declare Function SystemTimeToFileTime Lib "kernel32" ( _
ByRef lpSystemTime As SYSTEMTIME, _
ByRef lpFileTime As FILETIME) As Long
Public Const ZERO_DATE As Date = 0!
Public Function DateToFileTime(ByVal DateTime As Date) As FILETIME
Dim stLocal As SYSTEMTIME
Dim stUniversal As SYSTEMTIME
Dim ftResult As FILETIME
With stLocal
.wYear = Year(DateTime)
.wMonth = Month(DateTime)
.wDay = Day(DateTime)
.wHour = Hour(DateTime)
.wMinute = Minute(DateTime)
.wSecond = Second(DateTime)
End With
SystemTimeToFileTime stLocal, ftResult
DateToFileTime = ftResult
End Function
Public Function FileTimeToDate(ByRef FT As FILETIME) As Date
Dim stLocal As SYSTEMTIME
With FT
If (.dwHighDateTime <> 0) And (.dwLowDateTime <> 0) Then
FileTimeToSystemTime FT, stLocal
With stLocal
FileTimeToDate = DateSerial(.wYear, .wMonth, .wDay) _
+ TimeSerial(.wHour, .wMinute, .wSecond)
End With
Else
FileTimeToDate = ZERO_DATE
End If
End With
End Function
我不确定所有的虫子都被压扁了,但这应该足以让你开始。