Question Problem with DateTime matching.

priyamtheone

Well-known member
Joined
Sep 20, 2007
Messages
96
Programming Experience
Beginner
I'm trying to match a certain DateTime value with the current system DateTime. I have a timer and a label on a form. Timer interval is set to 1000. When the form loads, the timer starts ticking. As soon as the current DateTime matches the value of the variable, it shows a message in the label.

When I'm writing the following code, the values don't match even if the current system DateTime is equal to the variable. Label1 isn't showing 'Times Matched':

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim dtmNow As DateTime
dim dtmVar as DateTime=#04/28/2010 03:25:00 AM#

dtmNow = Now
If dtmVar = dtmNow Then Label1.Text = "Times Matched"
End Sub

But when I'm writing the same code by parsing 'Now' into DateTime it's working fine.

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim dtmNow As DateTime
dim dtmVar as DateTime=#04/28/2010 03:25:00 AM#

dtmNow = DateTime.Parse(Now.ToString)
If dtmVar = dtmNow Then Label1.Text = "Times Matched"
End Sub

Why is it so? The default format of 'Now' is the same as I have stored in dtmVar variable. So there's no question of format mismatch. Does that mean 'Now' is not actually a DateTime property?
Provided, my O.S. is Windows Vista Ultimate and all date/time settings are set to default.
Regards.
 
Your variable contains a time with a round second. What's the chance of the system time being exact to the second when you test it? You should check whether the current is greater than or equal to your target value, not just equal to.
 
Back
Top