Solved

Vb.Net - Compare Dates

Posted on 2013-05-16
9
322 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Show/Hide ASP.NET controls with JQuery best practices 3 99
Help with converting xml file to excel using VB.NET 3 25
RLDC Reporting in Visual studio 11 31
Visual Studios 1 78
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

821 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