English 中文(简体)
比较两个日期
原标题:Compare two date times
  • 时间:2010-01-05 08:26:03
  •  标签:
  • vb6

<代码>label1显示我通过查询从数据库获得的最后交易日期/时间。 <代码>label2为系统日期/时间。 我有一个时间来执行指挥塔顿,然后我想检查标签上的日期/时间是否小于5分钟。 如果是这样的话,我就想显示一场大屠杀。

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

我还尝试:

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
问题回答

如果从非行抽出的日期比现在更早,如果你作为第二个参数通过现在就永远会回负数。 它希望你重新检查时间的流逝,因此,我现在总是要看一下在非行的日期。 您需要改写现在的次序和与之比较的日期(<>DateDiff(n)>,日期1,现在,而不是DateDiff('n”,现在,日期1)

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

另一种不使用<代码>DateDiff功能的办法是直接计算。 日期:-variables - since under the hood these are actual 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

如果你想要检查时间差异,你也可以使用

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

在<代码>Abs(日期1-现在)上,时间差作为<代码>。 日期:

页: 1 自Now 回归Date - Value,Format换算成t- Value into a String,并将其分配给date1 将<代码>String 重新改为Date- Value ùsing the local system context fordate forms - 这意味着该方案将在不同的系统中运作。





相关问题
Prevent windows from queuing shellexecute requests

Win.ShellExecute 0, "open", "C:dirprogram.exe", "arguments", vbNullString, SW_SHOWNORMAL Win.ShellExecute 0, "open", "http://www.google.com", vbNullString, vbNullString, SW_SHOWNORMAL I want google....

Why is My Loop Only Deleting One File?

Using VB6 In a folder, i have n number of files, i want to delete a 0 kb files code Dim filename5 As String filename5 = Dir$(txtsourcedatabasefile & "*_*", vbDirectory) MsgBox filename5 Do ...

How to check the filesize?

Using VB6 I have the text file with different sizes, so i want to delete the file where filesize = 0 kb. How to make a vb6 code for deleting the 0 kb files. Need vb6 code Help

File Rename problem?

I m using VB6 and I have a folder where I have n number of files. I want to change the file extension to .txt. I used the code below to change the extension of all .fin files to .txt. Dim filename1 ...

Error 20728-F while in using Crystal Reports in VB6

I m using Crystal Reports in my VB6 project, but I m facing error while loading the report in crystalreport1.action=1; Please give me some solution for this problem. It is showing the error as Error ...

DllRegisterServer entry point was not found

When running my vb6 application I am getting error like, runtime error 53 : file not found: rscomclNoMsg.dll then i tried to register that dll from cmd line using regsvr32. Then I am getting ...

SQL Server 2000, ADO 2.8, VB6

How to determine if a Transaction is active i.e. before issuing Begin Transaction I want to ensure that no previous transaction are open.. the platform is VB6, MS-SQL Server 2000 and ADO 2.8

热门标签