简体   繁体   English

比较jquery timepicker的值

[英]compare jquery timepicker values

have timepricker set for 24hr as followers: 已将以下时间设置为24小时作为关注者:

$(document).ready(function(){
    $('input.timepicker').timepicker({ 
        timeFormat: 'HH:mm', 
        minTime: '04:00',
        interval: 10,
        scrollbar: true,
        dynamic: true
    });
});

I have two field to obtain start and finish times 我有两个字段来获取开始时间和结束时间

<input type="text" class="timepicker" name="travelstart" id="travelstart" placeholder="Travel Start" style="width:25%"> 
<input type="text" class="timepicker" name="travelfinish" placeholder="Travel Finish" style="width:25%" readonly/>

need some way of validating that finish time is later than start time. 需要某种方式来验证完成时间晚于开始时间。 something along the lines of: 类似于以下内容:

var ts = document.getElementById('travelstart')
var fs = document.getElementById('travelfinish')
if (ts.value => fs.value) { return false }

All you need to do is convert the time to a single unit and compare them. 您需要做的就是将时间转换为一个单位并进行比较。 Since it's just a 24h timepicker, we can use minutes here: 由于这只是24小时制,因此我们可以在此处使用分钟:

var ts = document.getElementById('travelstart').value;
var fs = document.getElementById('travelfinish').value;
var startTime, endTime, timeArr;
// split the hours and minutes
timeArr = ts.split(':');
// Hours is in timeArr[0]; minutes in timeArr[1];
startTime = (timeArr[0] * 60) + +timeArr[1];
// do the same for end time:
timeArr = fs.split(':');
endTime = (timeArr[0] * 60) + +timeArr[1];
// Now you can compare them
return startTime >= endTime;

I'm assuming you're using this timepicker jQuery plugin . 我假设您正在使用此timepicker jQuery插件

First of all if you call just document.getElementById('travelstart').value it returns time in format "04:00 AM" which is probably not what you want so better use: 首先,如果仅调用document.getElementById('travelstart').value则它将以"04:00 AM"格式返回时间,这可能不是您想要的,因此更好地使用它:

$('#options-change-event').timepicker().getTime()

to get an actual JavaScript Date object. 获取实际的JavaScript Date对象。

Then convert both times to timestamps and then compare it: 然后将两个时间都转换为时间戳,然后进行比较:

var tsDate = $('#options-change-event').timepicker().getTime();
var tsMinutes = 60 * tsDate.getHours() + tsDate.getMinutes();

// ... the same with the seconds timepicker

return tsMinutes > tfMinutes;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM