Vb6 比较两个日期时间

Vb6 比较两个日期时间,vb6,Vb6,label1显示我通过查询从数据库中获取的最后一个事务日期/时间。label2是系统日期/时间。我有一个计时器,它执行一个命令按钮,然后我想检查label1中的日期/时间是否小于5分钟。如果是这样的话,我想做个按摩 但是我不知道为什么我的代码不能执行这个函数。 任何帮助都将不胜感激 Private Sub Command1_Click() Dim date1 As Date Dim date2 As Date date1 = Format(Now, "yyyy/mm/d

label1显示我通过查询从数据库中获取的最后一个事务日期/时间。label2是系统日期/时间。我有一个计时器,它执行一个命令按钮,然后我想检查label1中的日期/时间是否小于5分钟。如果是这样的话,我想做个按摩

但是我不知道为什么我的代码不能执行这个函数。 任何帮助都将不胜感激

Private Sub Command1_Click()
    Dim date1 As Date
    Dim date2 As Date

    date1 = Format(Now, "yyyy/mm/dd hh:mm:ss")
    date2 = Format(label1, "yyyy/mm/dd hh:mm:ss")
    If DateDiff("n", date1, date2) < 2 Then
       MsgBox ("Not Vending")
    End If
End Sub
以及:

Private Sub Command1_Click()  
    If DateDiff("n", Now, label1) > 5 Then
       MsgBox ("Not Vending")
    End If
End Sub

如果从数据库中提取的日期早于现在,那么如果将现在作为第二个参数传递,则DateDiff将始终返回负数。看起来你在检查时间的流逝,所以我假设DB中的日期总是在现在之前。您需要切换“现在”和比较日期的顺序DateDiffn,date1,Now,而不是DateDiffn,Now,date1


另一种不使用DateDiff函数的方法是直接使用日期变量进行计算——因为在后台,这些变量实际上是双倍的

如果要检查时差,也可以使用

Private Sub Command1_Click()
    Dim date1 As Date
    date1 = CDate(Label1.Caption)
    '#12:05:00 AM# is representing a value of 5 Minutes in the Date-datatype
    If Abs(date1 - Now) < #12:05:00 AM# Then
       MsgBox ("Not Vending")
    End If
End Sub
其中Absdate1-现在将时间差作为日期值返回


顺便说一句,date1=FormatNow,yyyy/mm/dd hh:mm:ss没有意义。由于现在返回一个日期值,Format将该日期值转换为字符串,并将其分配给date1将该字符串转换回日期值,使用日期格式的本地系统设置-这意味着程序在不同系统上的行为不同。

label1从何而来?为什么将日期类型转换为字符串只是为了在字符串上调用DateDiff,从而强制它将字符串转换回日期??给我们多个版本的Command1_Click是没有帮助的,只给我们展示一个最接近您认为应该工作的版本。Raven的代码应该适合您。出于调试目的,我将添加和Else到语句中,如Else-MsgBox-DateDiffn、date1、Now&minutes-differenceThanks-For-reply将其排序。
Private Sub Command1_Click()
    Dim date1 As Date
    date1 = CDate(Label1.Caption)
    If DateDiff("n", date1, Now) < 5 Then
       MsgBox ("Not Vending")
    End If
End Sub
Private Sub Command1_Click()
    Dim date1 As Date
    date1 = CDate(Label1.Caption)
    '#12:05:00 AM# is representing a value of 5 Minutes in the Date-datatype
    If (date1 - Now) < #12:05:00 AM# Then
       MsgBox ("Not Vending")
    End If
End Sub
Private Sub Command1_Click()
    Dim date1 As Date
    date1 = CDate(Label1.Caption)
    '#12:05:00 AM# is representing a value of 5 Minutes in the Date-datatype
    If Abs(date1 - Now) < #12:05:00 AM# Then
       MsgBox ("Not Vending")
    End If
End Sub