locked
Date time compare RRS feed

  • Question

  • I’m a bit stuck I want to compare my last transaction date / time which display in a label which I get from the data base to the system date / time and if there is a deference of 5 minutes then I get a massage.

    The code that I’ve got check that the last transaction date is smaller but I don’t know how to set it so that it only check for 5 minutes.

     

    Help will be much appreciated’

    This is what I’ve got but it’s not doing what I expected

     

    [CODE]

    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

    [/CODE]

     

    Also

     

    [CODE]

    Private Sub Command1_Click()

     

    If DateDiff("n", Now, label1) > 5 Then

       MsgBox ("Not Vending")

     End If

    End Sub

    [/CODE]

    Tuesday, December 8, 2009 7:18 AM

Answers

  • Hi Cor

    wouldn't you need to check every second? ie make the Interval 1000

    the OP is including the seconds in the DateTime,

    date1 = Format(Now, "yyyy/mm/dd hh:mm:ss")
    
    



     so if it is only checking every 60 seconds, the chance of it doing a check at the exact second it turns to 5 minutes is extremely small

    then I think you also need to create a new DateTime Now without the milliseconds. I tried this just using Now, and it didn't catch the exact moment it hit 5 minutes because the milliseconds were not equal.

    Try this, but change date1 to a time that is about 4 minutes before Now. then run it and click the button. Let it run - it should stop when it hits 5 minutes


    Dim date1 As Date = #12/8/2009 12:05:00 AM#
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            
            Timer1.Interval = 1000
            Timer1.Enabled = True
        End Sub
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            Dim dtNow As DateTime = New Date(Now.Year, Now.Month, Now.Day, Now.Hour, Now.Minute, Now.Second)
            If dtNow.AddMinutes(-5) = date1 Then
                Timer1.Enabled = False
                MessageBox.Show(" 5 minutes ago")
            End If
        End Sub
    • Edited by jwavila Tuesday, December 8, 2009 8:14 AM
    • Marked as answer by Janos99 Tuesday, December 8, 2009 11:01 AM
    Tuesday, December 8, 2009 7:48 AM

All replies

  • Use a timer

    private withevents tm as new windows.Forms.Form.Timer

    private sub Load.................
    tm.Start
    tm.Interval = 60000 'so it checks every minute
    End Sub

    private Sub myTimerTicsk(sender As Object, _
                                               e As EventArgs) handles tm.Tick
    'Do you stuff here

    End sub 
    Success
    Cor
    Tuesday, December 8, 2009 7:32 AM
  • I think this will be a pretty impossible task

    what are the chances you will click the button at the exact second it is 5 minutes apart?
    Tuesday, December 8, 2009 7:39 AM
  • Hi Cor

    wouldn't you need to check every second? ie make the Interval 1000

    the OP is including the seconds in the DateTime,

    date1 = Format(Now, "yyyy/mm/dd hh:mm:ss")
    
    



     so if it is only checking every 60 seconds, the chance of it doing a check at the exact second it turns to 5 minutes is extremely small

    then I think you also need to create a new DateTime Now without the milliseconds. I tried this just using Now, and it didn't catch the exact moment it hit 5 minutes because the milliseconds were not equal.

    Try this, but change date1 to a time that is about 4 minutes before Now. then run it and click the button. Let it run - it should stop when it hits 5 minutes


    Dim date1 As Date = #12/8/2009 12:05:00 AM#
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            
            Timer1.Interval = 1000
            Timer1.Enabled = True
        End Sub
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            Dim dtNow As DateTime = New Date(Now.Year, Now.Month, Now.Day, Now.Hour, Now.Minute, Now.Second)
            If dtNow.AddMinutes(-5) = date1 Then
                Timer1.Enabled = False
                MessageBox.Show(" 5 minutes ago")
            End If
        End Sub
    • Edited by jwavila Tuesday, December 8, 2009 8:14 AM
    • Marked as answer by Janos99 Tuesday, December 8, 2009 11:01 AM
    Tuesday, December 8, 2009 7:48 AM
  • Thanks for the quick replay, The button is going to refresh the label every 5 minutes that is not the problem I need to check is the date in the label is standing still for 5 minutes because then I know that there is a problem.

    Tuesday, December 8, 2009 8:10 AM
  • Thanks i got it working
    Tuesday, December 8, 2009 11:16 AM
  • Why should you check every seconds as you need an interval from 5 minutes, as soon as it goes even 5 minutes is enough and the date testing is not needed again.

    So a little bit strange way like you do it to test every second if 5 minutes are gone, even that minute from me is in fact to long but I was afraid I would type something as 30000

    But unluckely for you this is marked as answer.

    It counts but everybody will see this in future

    :-)
    Success
    Cor
    Tuesday, December 8, 2009 11:35 AM