Vb.Net - Compare Dates

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
Jimbo99999Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Paul MacDonaldConnect With a Mentor Director, Information SystemsCommented:
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
 
Ioannis ParaskevopoulosCommented:
Hi,

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

Giannis
0
 
Atdhe NuhiuCommented:
Yes, do as above because as strings 1/1/2103 is less than 5/17/2013.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

 
Jimbo99999Author Commented:
"M/d/yyyy", "MM/dd/yyyy", "M/dd/yyyy", "MM/d/yyyy"
0
 
Meir RivkinConnect With a Mentor Full stack Software EngineerCommented:
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
 
Éric MoreauConnect With a Mentor Senior .Net ConsultantCommented:
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
 
Ioannis ParaskevopoulosConnect With a Mentor Commented:
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
 
Jimbo99999Author Commented:
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
 
Jimbo99999Author Commented:
Thanks everyone for your responses.  In my spare time I will try out the DateTimePicker control for future reference.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.