I am having a start time,start date and end date,end time on my web page.I need to ensure that if the start date and end date is equal then the end time must be greater than start time and if end date is greater than start date then user can able to enter any time.How can I achieve this validation?
-
how about using jquery'date plugin... – kobe Dec 10 '10 at 07:16
-
this question is close to your question http://stackoverflow.com/questions/833997/end-date-greater-than-start-date-jquery-validation – kobe Dec 10 '10 at 07:19
-
It would be good to know if there is any specific format your start date/time and end date/time are in. If there is some structure, an easy JavaScript solution can be used. Else, I'd advise a plugin like jQuery date or Datejs.com – dyve Dec 10 '10 at 07:46
-
@friends I am using http://jqueryui.com/demos/datepicker/ for date.But i am now with time. – kbvishnu Dec 10 '10 at 09:10
2 Answers
I would highly highly recommend something like Datejs. This should give you a starting point though:
// get the values from your form
var startDate = $('input[name=startDate]').val(),
endDate = $('input[name=endDate]').val(),
startTime = $('input[name=startTime]').val(),
endTime = $('input[name=endTime]').val()
If you decide to use Datejs you should parse the values from the input field using Date.parse(startDate)
. Check out the Getting Started. If you decide not to use DateJs, you could append getTime() to the startTime var. (e.g. - endTime.getTime() < startTime.getTime()
)
var error = '';
// if the start date is after the end date
if (startDate > endDate){
var error = error + 'Your start date is after your end date.';
}
// if the start date and end date are the same -- and end time is before the start time
else if(startDate == endDate && endTime < startTime){
var error = error + 'Your end time is before your start time.';
};
// handle any errors
if(error.length){
alert(error);
};

- 5,435
- 9
- 45
- 64
-
-
both the start time and end time are entered mannuvally by user. it may be in the format like 12.55am or 01.22pm etc and the dates are in the format of dd/mm/yy.That also entered by user – kbvishnu Dec 10 '10 at 09:17
It's good to know how stuff works, and you could benefit from building this yourself without depending on a framework. Should you want to try it, assuming you have simple strings for your dates and times, you could start with this:
var dateStart = "Oct 13, 2010";
var timeStart = "10:13";
var dateEnd = "Nov 11, 2011";
var timeEnd = "14:56";
var startDate = new Date(dateStart + " " + timeStart);
var endDate = new Date(dateEnd + " " + timeEnd);
alert(startDate > endDate);
For real code see http://jsbin.com/oponu3/edit
If you are just using this to compare time, then this would work:
var myDate = "Oct 13, 2010";
var timeStart = "10:13";
var timeEnd = "14:56";
var startDate = new Date(myDate + " " + timeStart);
var endDate = new Date(myDate + " " + timeEnd);
alert(startDate > endDate);
You could enhance this by checking for illegal date values. Check the W3C reference for the JavaScript Date object for this: http://www.w3schools.com/js/js_obj_date.asp
// calculate difference (added 2012-04-04)
var milliseconds = Math.abs(startDate.getTime() - endDate.getTime());
var seconds = milliseconds / 1000;
var minutes = seconds / 60;
var hours = minutes / 60;
// etc etc etc

- 5,893
- 2
- 30
- 44
-
-
-
+1. Solves half my purpose. Is there any way we can check the time difference between the 2 dates (including time) to be greater than 24 hours? That will really help me if you can answer this. Thank you. – Devner Feb 27 '12 at 18:32
-
var milliseconds = Math.abs(startDate.getTime() - endDate.getTime()); var seconds = milliseconds / 1000; var minutes = seconds / 60; var hours = minutes / 60; // etc etc etc – dyve Apr 04 '12 at 07:23