Private Sub Command1_Click()
Dim dteNow As Date
    dteNow = CDate("19:59:59") + TimeSerial(0, 0, 1) ''这里的结果是20:00:00吧?
    If FoundInTime(dteNow) = 1 Then
        MsgBox "Found It"
    End If
End SubPublic Function FoundInTime(ByVal dteNow As Date) As Long
Dim dteStart    As Date
Dim dteEnd      As Date
    If IsDate(dteNow) Then
        dteStart = DateValue(dteNow) + CDate("20:00:00")
        dteEnd = DateValue(dteNow) + CDate("23:59:59")
        If dteStart <= dteNow And dteNow < dteEnd Then  
           ''不理解上边的判断为什么会失败! 程序不会进到这里来!
           ''可是dteStart明明是20:00:00, dteNow也是20:00:00
           ''dteStart<=dteNow 应该是True, 快速信息查询显示为False!!!!!
            FoundInTime = 1
        End If
    End If
End Function

解决方案 »

  1.   

    Private Sub Command1_Click()
    Dim dteNow As Date
        dteNow = CDate("19:59:59") + TimeSerial(0, 0, 1) ''这里的结果是20:00:00吧?
        If FoundInTime(dteNow) = 1 Then
            MsgBox "Found It"
        End If
    End SubPublic Function FoundInTime(ByVal dteNow As Date) As Long
    Dim dteStart    As Date
    Dim dteEnd      As Date
        If IsDate(dteNow) Then
            dteStart = DateValue(dteNow) + CDate("20:00:00")
            dteEnd = DateValue(dteNow) + CDate("23:59:59")
            If Format(dteStart, "hh:mm:ss") <= Format(dteNow, "hh:mm:ss") And dteNow < dteEnd Then'加上format来判断
               ''不理解上边的判断为什么会失败! 程序不会进到这里来!
               ''可是dteStart明明是20:00:00, dteNow也是20:00:00
               ''dteStart<=dteNow 应该是True, 快速信息查询显示为False!!!!!
                FoundInTime = 1
            End If
        End If
    End Function
      

  2.   

    Dim dteNow As Date是command1Click事件中的一个局部变量,其作用范围仅限于该过程内部,将该语言放在所有的事件之前,即通用声明部分即可
    dim dteNow as data
    private sub ......
      

  3.   

    二进制小数的运算和表达问题。dteStart明明是20:00:00, dteNow也是20:00:00,但是表示它们的二进制小数并不相等。If format(dtestart,"HH:nn:ss")<=format(dtenow,"HH:nn:ss")And dteNow < dteEnd Then
    可以消除二进制小数表示十进制小数的微小差异。If CSng(dteStart) <= CSng(dteNow) And dteNow < dteEnd Then
    也可以。判断二进制小数相等一定要谨慎。最好是用大于、小于来处理。
      

  4.   

    我试着将其变成其它类型时,发现如果变成Decimal 子类型的话,两个值会不一样,但为什么会不一样,就不是太清楚了.
    ?cdec(dtestart)
     .833333333333334 
    ?cdec(dtenow)
     .833333333333333
      

  5.   

    to: lzlyh(Ghost.Snake):
       看来该学习变量生存周期的人是你啊, 这段程序中变量生存周期是绝对没问题的,而且相当地标准. to of123():
       大侠说得很有道理to xingnup(黑猫):
       大侠给出了具体的比较结果, 证实了of123()大侠的观点.看看其他大侠是不是还有高见, 没有的话就结帐喽
      

  6.   

    Option Explicit
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)Private Sub Command1_Click()
    Dim dteNow As Date
        dteNow = CDate("19:59:59") + TimeSerial(0, 0, 1) ''这里的结果是20:00:00吧?
        If FoundInTime(dteNow) = 1 Then
            MsgBox "Found It"
        End If
    End SubPublic Function FoundInTime(ByVal dteNow As Date) As Long
    Dim dteStart    As Date
    Dim dteEnd      As Date
        If IsDate(dteNow) Then
            dteStart = DateValue(dteNow) + CDate("20:00:00")
            dteEnd = DateValue(dteNow) + CDate("23:59:59")
            If dteStart = dteNow Then
                FoundInTime = 1
            End If
        End If
        Call PrintDate("dteStart", dteStart)
        Call PrintDate("dteNow", dteNow)
    End FunctionPrivate Function PrintDate(strName As String, dtPrintMe As Date)
    Dim byt(7) As Byte, i As Integer
    Dim strPrint As String
    CopyMemory byt(0), dtPrintMe, 8
    For i = 0 To 7
        strPrint = strPrint & " " & CLng(byt(i))
    Next
    Debug.Print strName & "=", strPrint
    End Function输出:
    dteStart=      171 170 170 170 170 170 234 63
    dteNow=        170 170 170 170 170 170 234 63运行后发现 最低位 不一样,一个为1 一个为0 msdn上的信息:
    当其他的数值类型要转换为 Date 型时,小数点左边的值表示日期信息,而小数点右边的值则表示时间。午夜为 0 而中午为 0.5。负整数表示 1899 年 12 月 30 日之前的日期。由于DateValue(dteNow)返回"0:00:00"
    所以小弟以为
    dteStart 是第2天的 20:00:00
    dteNow 是今天的 20:00:00
      

  7.   

    to baoaya(点头) 大侠:
    你说:
    由于DateValue(dteNow)返回"0:00:00"
    所以小弟以为
    dteStart 是第2天的 20:00:00
    dteNow 是今天的 20:00:00这没道理啊, 我的程序:
        If IsDate(dteNow) Then
            dteStart = DateValue(dteNow) + CDate("20:00:00")
            ....
            ''这里dteStart不是已经取了DateValue(dteNow)的日期值了吗? 所以他们的日期是一样的!
      

  8.   

    Private Sub Command1_Click()
    MsgBox DateValue("20:00:00")
    End Sub就会弹出 0:00:00