Here the code
$(document).ready(function() {
$('#st').change(function(){
var st = $('#st').val(); // start time Format: '9:00 PM'
var et = $('#et').val(); // end time Format: '11:00 AM'
//how do i compare time
if(st > et)
{
alert('end time always greater then start time');
}
});
});
if i have time range as follows
Start Time Range (listbox) = 12:00 AM To 11:59PM
End Time Range (listbox) = 12:00 AM To 11:59PM
then how to validate start-time less then end-time OR end-time greater then start-time
Start-time < End-time OR End-time > St-time
consider time format in js-code, due to time-format am unable to implement difference logic
Format: '9:00 AM'
Format: '5:00 PM'
Here i got after investigate this.
The best practice if Time Format in 12 Hour-Time Or 24 Hour-Time.
//start time
var start_time = $("#start_time").val();
//end time
var end_time = $("#end_time").val();
//convert both time into timestamp
var stt = new Date("November 13, 2013 " + start_time);
stt = stt.getTime();
var endt = new Date("November 13, 2013 " + end_time);
endt = endt.getTime();
//by this you can see time stamp value in console via firebug
console.log("Time1: "+ stt + " Time2: " + endt);
if(stt > endt) {
$("#start_time").after('<span class="error"><br>Start-time must be smaller then End-time.</span>');
$("#end_time").after('<span class="error"><br>End-time must be bigger then Start-time.</span>');
return false;
}
You have to convert strings to values you can compare in terms of time. Date.parse
comes in handy, though you're required to pass a date as well. Since you only care about time, use the same fake date for both.
if(Date.parse("1-1-2000 " + st) > Date.parse("1-1-2000 " + et)) {
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With