Solved

VB.NET Calculate Date Difference

Posted on 2008-10-10
9
3,484 Views
Last Modified: 2012-05-05
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?
0
Comment
Question by:CipherIS
9 Comments
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
Comment Utility
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
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
Comment Utility
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
 
LVL 1

Author Comment

by:CipherIS
Comment Utility
It does not compare correctly so it always displays the message box.
0
 
LVL 8

Expert Comment

by:tiagosalgado
Comment Utility
Try to watch your date values and check if they are in same format. Maybe there is your problem.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 1

Author Comment

by:CipherIS
Comment Utility
Still does not worked.  Attached file.
Date.JPG
0
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
Comment Utility
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
 
LVL 48

Accepted Solution

by:
jpaulino earned 500 total points
Comment Utility
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
 
LVL 1

Author Closing Comment

by:CipherIS
Comment Utility
Works as expected
0
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
Comment Utility
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

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now