How can I compare date/time values using the jQueryUI datepicker and HTML5 time input?

后端 未结 1 1591
臣服心动
臣服心动 2021-01-21 13:41

I want to validate that the \"begin\" date/time pair predates the \"end\" date/time pair on a page. I\'m using the jQueryUI datepicker, and the HTML5 time input element/widget.<

相关标签:
1条回答
  • 2021-01-21 14:17

    Here's an approach. You need to test to see if the dates are the same, and then inside that, append the times to new date objects that you can then compare. (There might be a faster way to do this, but this works in testing, here: http://jsfiddle.net/mori57/SEqVE/):

    } else if(begD.toString() == endD.toString() ){
        var dteString = begD.getFullYear() + "/" + (begD.getMonth()+1) + "/" + begD.getDate();
        var begT = new Date(dteString + " " + $('#BeginTime').val());
        var endT = new Date(dteString + " " + $('#EndTime').val());
    
        if( begT > endT ){
            alert('Begin date must be before End date');
            $('#BeginTime').focus();
            return false;
        }
    }
    
    0 讨论(0)
提交回复
热议问题