VB.NET Calculate Date Difference

I know there must be an easier way to calculate difference between dates or if one date is less than another.  Problem is I am trying to calculate a date in at text box against Date.Now.

This does not work.  
If CDate(Me.TextBox1.Text) < Date.Now Then
  MessageBox.Show("Date is less than" & Date.Now, "Date", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If

This also does not work.  
Dim strDateDiff As String = DateDiff(DateInterval.Day.ToString(), Me.TextBox1.Text, Date.Now)

Any ideas?
LVL 1
CipherISAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Jorge PaulinoConnect With a Mentor IT Pro/DeveloperCommented:
Try this method substracting the dates:
        Dim dt As New DateTime
 
        ' checks if it's a valid date
        If DateTime.TryParse(Me.TextBox1.Text, dt) Then
 
            ' Get's the number of days
            Dim days As Integer = dt.Subtract(Now.Date).TotalDays
 
            ' Writes the result
            Select Case days
                Case 0
                    Debug.WriteLine("TextBox1 = Now")
                Case Is > 0
                    Debug.WriteLine(String.Format("TextBox1 > Now in {0} days", days.ToString))
                Case Is < 0
                    Debug.WriteLine(String.Format("TextBox1 < Now in {0} days", days.ToString))
            End Select
 
        End If

Open in new window

0
 
Wayne Taylor (webtubbs)AstronautCommented:
Hi CipherIS,

Use the DateTime.Compare method -> http://msdn.microsoft.com/en-us/library/system.datetime.compare(VS.80).aspx

        If Date.Compare(CDate(Me.TextBox1.Text), Date.Now) < 0 Then
            MessageBox.Show("Date is less than" & Date.Now, "Date", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If

Regards,

Wayne
0
 
Wayne Taylor (webtubbs)AstronautCommented:
I'd also be interested to know what you mean by "does not work". Is it producing any errors, or does it return unexpected results?

Wayne
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
CipherISAuthor Commented:
It does not compare correctly so it always displays the message box.
0
 
tiagosalgadoCommented:
Try to watch your date values and check if they are in same format. Maybe there is your problem.
0
 
CipherISAuthor Commented:
Still does not worked.  Attached file.
Date.JPG
0
 
Wayne Taylor (webtubbs)AstronautCommented:
CipherIS,

If the textbox has todays date in it, it will always be less than Date.Now, because it also takes the time into account.

Wayne
0
 
CipherISAuthor Commented:
Works as expected
0
 
Wayne Taylor (webtubbs)AstronautCommented:
CipherIS,

I'm curious why you selected jpaulino's suggestion over mine? I mentioned the reason why it was saying it was less than Date.Now at http:#a22686583

Wayne
0
All Courses

From novice to tech pro — start learning today.