Comparing Dates In VB.NET

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gderosa
    New Member
    • Nov 2006
    • 23

    Comparing Dates In VB.NET

    I searched before posting and followed instructions but it still did not work for me. I need to compare 2 dates but regardless of what the dates are they always return the same value on comparison. Here is my code:

    Code:
    Dim EndDate As DateTime
    Dim Current As DateTime = Today
    
    If DateTime.Compare(Current, EndDate) < 0 Then
        Label6.Text = "CONTRACT EXPIRED LESS THAN"
    End If
    If DateTime.Compare(Current, EndDate) = 0 Then
        Label6.Text = "CONTRACT EXPIRED EQUALS"
    End If
    If DateTime.Compare(Current, EndDate) > 0 Then
        Label6.Text = "CONTRACT NOT EXPIRED GREATER"
    End If
    The EndDate is being pulled in from a database and no matter what the end date is whether is be greater or less than todays day is always displays as CONTRACT NOT EXPIRED GREATER. Anyone have an idea of what I am doing wrong? Thanks very much in advance!
  • kenobewan
    Recognized Expert Specialist
    • Dec 2006
    • 4871

    #2
    I suggest displaying the results first and see if there is a fortmatting problem, especially the result from the db.

    Hope that this helps.

    Comment

    • digitalmanic
      New Member
      • Oct 2006
      • 24

      #3
      http://blogs.vbcity.co m/xtab/archive/2005/12/26/5755.aspx

      Comment

      • ashima515
        New Member
        • Nov 2006
        • 40

        #4
        Hi

        Use datediff() function.Pls search on google to know more about datediff() function.

        Hope this helps you.
        Happy coding...

        Originally posted by gderosa
        I searched before posting and followed instructions but it still did not work for me. I need to compare 2 dates but regardless of what the dates are they always return the same value on comparison. Here is my code:

        Code:
        Dim EndDate As DateTime
        Dim Current As DateTime = Today
        
        If DateTime.Compare(Current, EndDate) < 0 Then
            Label6.Text = "CONTRACT EXPIRED LESS THAN"
        End If
        If DateTime.Compare(Current, EndDate) = 0 Then
            Label6.Text = "CONTRACT EXPIRED EQUALS"
        End If
        If DateTime.Compare(Current, EndDate) > 0 Then
            Label6.Text = "CONTRACT NOT EXPIRED GREATER"
        End If
        The EndDate is being pulled in from a database and no matter what the end date is whether is be greater or less than todays day is always displays as CONTRACT NOT EXPIRED GREATER. Anyone have an idea of what I am doing wrong? Thanks very much in advance!

        Comment

        • gderosa
          New Member
          • Nov 2006
          • 23

          #5
          Great thanks very much for your help I got it to work!

          Comment

          Working...