Solved

Vb.Net - Compare Dates

Posted on 2013-05-16
9
319 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
Comment Utility
Hi,

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

Giannis
0
 
LVL 33

Accepted Solution

by:
paulmacd earned 300 total points
Comment Utility
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
Comment Utility
Yes, do as above because as strings 1/1/2103 is less than 5/17/2013.
0
 

Author Comment

by:Jimbo99999
Comment Utility
"M/d/yyyy", "MM/dd/yyyy", "M/dd/yyyy", "MM/d/yyyy"
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 42

Assisted Solution

by:sedgwick
sedgwick earned 150 total points
Comment Utility
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 69

Assisted Solution

by:Éric Moreau
Éric Moreau earned 25 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks everyone for your responses.  In my spare time I will try out the DateTimePicker control for future reference.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

762 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

8 Experts available now in Live!

Get 1:1 Help Now