最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery-validating start time and end time selected using time picker - Stack Overflow

programmeradmin8浏览0评论

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.');
    }
});

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.');
    }
});
Share Improve this question edited Dec 27, 2012 at 11:22 Midhun asked Dec 27, 2012 at 9:36 MidhunMidhun 271 gold badge3 silver badges10 bronze badges 4
  • Show your code. where is you if statement? – Shiplu Mokaddim Commented Dec 27, 2012 at 9:38
  • you can use 24-hr format for time. This will resolve your problem for paring. – anuj arora Commented Dec 27, 2012 at 9:44
  • I cant use 24 hr format for time.I need 12 hr itself. – Midhun Commented Dec 27, 2012 at 9:46
  • I am not getting when i am using localeCompare. Is there any other way? – Midhun Commented Dec 27, 2012 at 10:04
Add a ment  | 

2 Answers 2

Reset to default 3

First convert to lowest denominational (minute here). Then pare 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;
}

please try that below code in case you have length of time more than 6 char like 10:00am instead of 9:00am will throw error above code.

I did minor changes in above code and it worked like charm for me.

$('#btnSubmit').on('click', function () {
           
            var startTime = $('#txtJobStartTime').val();
            var endTime = $('#txtJobEndTime').val();
           
            var st = minFromMidnight(startTime);
            var et = minFromMidnight(endTime);
            if (st > et) {
                alert('End time always greater then start time.');
                return false;
            }

            function minFromMidnight(tm) {
                var ampm = tm.substr(-2);
                var clk;
                if (tm.length <= 6) {
                    clk = tm.substr(0, 4);
                } else {
                    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;
            }
        });

发布评论

评论列表(0)

  1. 暂无评论