Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
151
rated 0 times [  155] [ 4]  / answers: 1 / hits: 17491  / 12 Years ago, thu, december 27, 2012, 12:00:00

I want to validate start time and end time selected using timepicker which is in 12 hr format. The end time should be greater than the start time. I used an if statement but when the test is using values such as 8:00 AM as start time and 1:00 PM as end time, it is not working. What can I do to solve this problem. Someone please help me with this. I am stuck with this since yesterday. I want just time ,i don't need date.



$(#dateTimeAddButton).click(function () 
{
var Date=$('#myDatePickerId').val()
var startTime = $('#i').val();
var endTime = $('#i1').val();
if (startTime > endTime)
{
alert('End time always greater then start time.');
}
});

More From » jquery

 Answers
96

First convert to lowest denominational (minute here). Then compare it.



st = minFromMidnight(startTime);
et = minFromMidnight(endTime);
if(st>et){
alert(End time must be greater than start time);
}

function minFromMidnight(tm){
var ampm= tm.substr(-2)
var clk = tm.substr(0, 5);
var m = parseInt(clk.match(/d+$/)[0], 10);
var h = parseInt(clk.match(/^d+/)[0], 10);
h += (ampm.match(/pm/i))? 12: 0;
return h*60+m;
}

[#81207] Tuesday, December 25, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ignacio

Total Points: 467
Total Questions: 128
Total Answers: 79

Location: Luxembourg
Member since Tue, Mar 14, 2023
1 Year ago
;