Compare two date times

♀尐吖头ヾ 提交于 2020-01-24 12:24:48

问题


label1 displays the last transaction date/time which I get from a database through a query. label2 is the system date/time. I have a timer that executes a command button after which I want to check if the date/time in label1 is smaller than 5 minutes. If so then I want to show a massage.

But I don’t know why my code is failing to perform this function. Any help will be much appreciated.

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

I've also tried:

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

As well as:

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

回答1:


If the date pulled from the DB is earlier than Now, DateDiff will always return a negative number if you pass Now as the second parameter. It looks like you're checking for the passage of time, so I'll assume the dates in DB will always be before Now. You need to switch the order of Now and the date to which it is being compared (DateDiff("n", date1, Now) instead of 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



回答2:


An other way without using the DateDiff-function would be direct calculation with the Date-variables - since under the hood these are actually Doubles.

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

If you want to check the time difference you can also use

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

Where Abs(date1 - Now) returns the time difference as Date-value

Btw. date1 = Format(Now, "yyyy/mm/dd hh:mm:ss") doesn't make sence. Since Now returns a Date-value, Format converts that Date-value into a String and assigning it to date1 converts that String back to a Date-value ùsing the local system settings for date formats - which means that the program will behave different on different systems.



来源:https://stackoverflow.com/questions/2004788/compare-two-date-times

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!