Solved

Validating two dates in ASP.net C#

Posted on 2006-10-31
5
301 Views
Last Modified: 2012-06-27
Hello Experts,

In our Meeting schedule application, we have two date fields, Meeting Start Date/Time and Meeting End Date/Time.

How do I validate the two date/time such that, end date/time cannot be prior to StartDate/Time and StartDate time cannot be prior to currentDatetime.

DatTime is a usercontrol which contains a textbox and a reqd field validator to check null.

Thanks in advance for your help
0
Comment
Question by:rowmark
5 Comments
 
LVL 35

Expert Comment

by:YZlat
ID: 17843470
use CompareValidator
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 200 total points
ID: 17843471
One way would be to create a custom date validator.

Here are some examples:

Writing a Custom Validator Control in ASP.NET
http://www.c-sharpcorner.com/Code/2002/May/DateCustomValidator.asp

Date Validator Custom Web Control
http://www.codeproject.com/aspnet/datevalidator.asp

Bob
0
 
LVL 10

Assisted Solution

by:jnhorst
jnhorst earned 200 total points
ID: 17843602
The DateTime class has a Subtract() method that returns a TimeSpan object.  Your code would look something like this:

DateTime start = DateTime.Parse("the date in your start textbox");
DateTime end = DateTime.Parse("the date in your end textbox");
TimeSpan diff = end.Subtract(start);

if (diff.TotalDays < 0)
{
     //... code would get here if end was earlier than start.
}

You can do the same with the start date and the current date, like this:

TimeSpan startDiff = start.Subtract(DateTime.Now);

if (startDiff.TotalDays < 0)
{
     // code would get here if the start day was before today.
}

John
0
 
LVL 6

Assisted Solution

by:Roopesh_7
Roopesh_7 earned 100 total points
ID: 17843869
hi,
if there is only reqd field validator to check null then first you need to check the enterd date is valid or not....i think better to use a client side function for that to validate date then you can doo the above stuf....
0
 

Author Comment

by:rowmark
ID: 17844340
Thank you all for your help.

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
A short film showing how OnPage and Connectwise integration works.
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

947 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

21 Experts available now in Live!

Get 1:1 Help Now