Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 363
  • Last Modified:

Validating two dates in ASP.net C#

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
rowmark
Asked:
rowmark
3 Solutions
 
YZlatCommented:
use CompareValidator
0
 
Bob LearnedCommented:
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
 
jnhorstCommented:
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
 
Roopesh_7Commented:
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
 
rowmarkAuthor Commented:
Thank you all for your help.

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now