Date thing problem?

How do I make my progam in vb.net to check the date and time in regular inteval and having to compare those to a surtain string like: Dim datee As String datee = "13.06.2006" . I'd like to use the: Today and TimeOfDay (i dont know how to name them, strings maybe )...

Any help about this because I was trying t figure it out but it does not work. I tried using timers and stuff but it didnt work, I also tried searching MSDN for that...

I hope you know somethig about this, I want to apply for Coding4Fun and I'll probably need this for april fools day, for a prank to upload to Coding4Fun.




Answer this question

Date thing problem?

  • Deepu_a

    Thanks. Why doesnt my Mark As Answer Button isnt there

  • Adam23

    There are lots of ways you could do this. It also depends on the frequency that you want to check the time/date.

    If it is an application that you would be starting when you start your computer it would look something like this:

    1. Window form with a timer on it. You would set the timer to how often you want the comparision to occur.

    2. Create a private variable for your date.

    Dim dateToCompare as Date = Date.Parse("05/06/2006 12:15:52") 'You date value could be a text box value or a datetime picker value.

    3. In the timer_Tick EventHandler method compare the current system to your time.

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles Timer1.Tick
    If Date.Compare(
    dateToCompare, Now) > 1 Then
    'what ever you want to do when the date has occured

    MessageBox.Show("The event has happened.")
    End If
    End Sub

  • rwerner

    The following will work. The reason you cant use timeofday is because it is down to the millisecond and trying to use a = comparison with this level of precision will invariably result in no match being found.

    What I'm doing in the timer tick event is checking the hour, minute and second between the alarm time and current time and if they all match then alarm msgbox appears. The timer interval is set to 1000 so this occurs event 1 second and should be OK for this level of precision.

    Public Class Form1
    Dim alarmtime As Date

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Timer1.Interval = 1000
    Timer1.Enabled = True

    alarmtime = DateAdd(DateInterval.Second, 10, Now) '//Alarm time is 10 seconds time.
    End Sub


    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    '//Time of Day is down to milliseconds so cant do a direct = comparison
    If (alarmtime.Hour = Now.Hour) And (alarmtime.Minute = Now.Minute) And (alarmtime.Second = Now.Second) Then
    MsgBox("Alarm Set")
    End If
    End Sub
    End Class


  • Marcos B

    Date.Compare(dateToCompare, Now) is not the same as Date.Equal.

    .Compare returns an integer value -1 for less than, 0 for equal, and 1 for greater than. hence the
    Date.Compare(dateToCompare, Now) > 1

  • Date thing problem?