Problem with DateTime matching.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • priyamtheone
    New Member
    • Sep 2007
    • 88

    Problem with DateTime matching.

    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':
    Code:
    	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.
    Code:
    	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.
    Last edited by Frinavale; May 19 '10, 03:00 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags.
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    DateTime.Now goes down to the 3 digits of milliseconds, regardless of how you display it. So getting an exact match of .Now is going to be (literally) a one in a thousand chance.

    You'd be better off taking the time as a string, trimmed to hours, minutes, seconds and comparing that.

    DateTime.Now.To String("HH:mm:s s"); // To get 13:30:45
    DateTime.Now.To String("HH:mm:s s.fff"); // To include miliseconds 13:30:45.678

    Comment

    • priyamtheone
      New Member
      • Sep 2007
      • 88

      #3
      Thanks for the idea. It's really helpful. This is what I ended up doing:

      Dim dtmNow As DateTime
      dim dtmVar as DateTime=#04/28/2010 03:25:00 AM#
      dtmNow = (Now.ToString(" MM/dd/yyyy hh:mm:ss tt"))
      If dtmVar = dtmNow Then Label1.Text = "Times Matched"

      The other way is to Parse the current DateTime value as I posted.

      Comment

      • Curtis Rutland
        Recognized Expert Specialist
        • Apr 2008
        • 3264

        #4
        Another simple way to do this is to subtract the two DateTime values, and see how many seconds the resulting TimeSpan object contains.

        Code:
        DateTime dt1 = new DateTime(2010, 5, 12, 9, 38, 0);
        TimeSpan difference = dt1.Subtract(DateTime.Now);
        double seconds = System.Math.Abs(difference.TotalSeconds);
        If seconds < 1 then it is equivalent.

        Edit, sorry for the C# code, but it should be pretty self explanatory in VB.NET.

        Comment

        • priyamtheone
          New Member
          • Sep 2007
          • 88

          #5
          Thank you very much.

          Comment

          Working...