vb6 - 比较两个日期时间

标签 vb6

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()
    Dim date1 As Date
    Dim label1 As Date

    date1 = Format(Now, "yyyy/mm/dd hh:mm:ss")
    date2 = label1
    If DateDiff("m", Now, date1) > DateDiff("m", Now, label1) 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

最佳答案

如果从数据库中提取的日期早于 Now,并且将 Now 作为第二个参数传递,则 DateDiff 将始终返回负数。看起来您正在检查时间的流逝,因此我假设 DB 中的日期始终在 Now 之前。您需要切换 Now 及其比较日期的顺序(DateDiff("n", date1, Now) 而不是 DateDiff("n", Now, date1) .

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

关于vb6 - 比较两个日期时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2004788/

相关文章:

vb6 - 本地货币字符串转换

c# - 相当于 C# 中 VB6 中的 string * 10

animation - 如何在VB6中流畅地制作这个角色的动画

c# - 将 VB6 变量转换为 C#

ms-word - 如何将 Microsoft Word 设置为 VB6 表单的所有者?

vb6 - 仅在文本框中允许数字值

vb6 - 如何在 VB6 中重新初始化 UDT?

asynchronous - 如何使用 Chilkat 作为异步通过 VB6 从网站获取数据?

VB.NET 相当于 VB6 属性 Item.VB_UserMemId = 0

vb6 - 在 VB6 中将 Double 拆分为 Long loword 和 Long hiword