Solved

Vb.Net - Compare Dates

Posted on 2013-05-16
9
321 Views
Last Modified: 2013-05-16
Good Day Experts!

I have two textboxes that accept start and end dates.  I have a routine that checks if the dates are formatted in acceptable formats.  That part works great.  The last step of the routine I want to check if the start date is less than the end date.  However, I am apparently doing something wrong since 1/1/2103 is not flagged as greater than 5/17/2013.  Here is my compare:

If txtStartPW.Text.Trim > txtEndPW.Text.Trim Then
   ValidationResult = "Start Date is not less than End Date. Please revise your entry."
End If

It "feels" like it is going to be something related to the values being strings and not comparing the actual dates...but, I am not good with coding around date stuff.

Can you help?

Thanks,
jimbo99999
0
Comment
Question by:Jimbo99999
9 Comments
 
LVL 23

Expert Comment

by:Ioannis Paraskevopoulos
ID: 39171346
Hi,

What are the valid formats? Is it only M/D/YYYY?

Giannis
0
 
LVL 34

Accepted Solution

by:
Paul MacDonald earned 300 total points
ID: 39171351
If CDate(txtStartPW.Text.Trim)CDate(txtEndPW.Text.Trim) Then
   ValidationResult = "Start Date is not less than End Date. Please revise your entry."
End If


...or something to that affect.
0
 
LVL 13

Expert Comment

by:Philip Pinnell
ID: 39171357
Yes, do as above because as strings 1/1/2103 is less than 5/17/2013.
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

Author Comment

by:Jimbo99999
ID: 39171358
"M/d/yyyy", "MM/dd/yyyy", "M/dd/yyyy", "MM/d/yyyy"
0
 
LVL 42

Assisted Solution

by:sedgwick
sedgwick earned 150 total points
ID: 39171364
If DateTime.Parse(txtStartPW.Text.Trim) > DateTime.Parse(txtEndPW.Text.Trim) Then
   ValidationResult = "Start Date is not less than End Date. Please revise your entry."
End If

Open in new window

0
 
LVL 70

Assisted Solution

by:Éric Moreau
Éric Moreau earned 25 total points
ID: 39171373
why reinvent the wheel. You should use the DateTimePicker control to input dates. When comes time to compare, as already mentioned, you need to compare dates and not text.
0
 
LVL 23

Assisted Solution

by:Ioannis Paraskevopoulos
Ioannis Paraskevopoulos earned 25 total points
ID: 39171387
Add :
Imports System.Globalization

Open in new window

and try this:
MsgBox(DateTime.ParseExact("5/17/2013", "M/d/yyyy", a) > DateTime.ParseExact("1/1/2103", "M/d/yyyy", a))

Open in new window


Giannis
0
 

Author Comment

by:Jimbo99999
ID: 39171398
We actually did consider that. But, after polling the User community they voted on manual entry.  Most of them were former Data-entry people so they are pretty quick on the number keypad.
0
 

Author Closing Comment

by:Jimbo99999
ID: 39171417
Thanks everyone for your responses.  In my spare time I will try out the DateTimePicker control for future reference.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Variable Event ? 3 27
VB: Convert 2 dates to specific format 24 48
Import a txt file into a DataGridView and TextBox 20 34
SqlServer no dupes 25 34
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

805 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