Solved

Validating two dates in ASP.net C#

Posted on 2006-10-31
5
289 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
Comment Utility
use CompareValidator
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 200 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you all for your help.

0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
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.…
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…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

772 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

11 Experts available now in Live!

Get 1:1 Help Now