Solved

VB.NET Calculate Date Difference

Posted on 2008-10-10
9
3,486 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)
ID: 22686457
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)
ID: 22686474
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
ID: 22686508
It does not compare correctly so it always displays the message box.
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 8

Expert Comment

by:tiagosalgado
ID: 22686546
Try to watch your date values and check if they are in same format. Maybe there is your problem.
0
 
LVL 1

Author Comment

by:CipherIS
ID: 22686562
Still does not worked.  Attached file.
Date.JPG
0
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 22686583
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
ID: 22686680
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
ID: 31504992
Works as expected
0
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 22686812
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Web Form VB.Net  import CSV 4 36
Help with exporting to excel 4 25
VB.NET 2008 Publish Error 2 24
vb.net and creating a class 5 14
This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

813 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

12 Experts available now in Live!

Get 1:1 Help Now