我试图计算轮班的持续时间,它对于白天和晚上的轮班时间都适用,但是y会返回错误。在控制台中,它会返回意外的标识符错误,但是代码可以正常工作并返回答案。认为if部分可能存在问题。但是据我所知,我找不到任何东西
$("#formShiftEndTime").on("change", function() {
var startHour = $("#formShiftStartTime").val();
var endHour = $("#formShiftEndTime").val();
calculate_time(startHour, endHour);
// console.log(startHour);
})
function showTime(hours, minutes) {
return ((hours < 10) ? '0' : '') + hours +
':' +
((minutes < 10) ? '0' : '') + minutes;
}
var startHour = $("#formShiftStartTime").val();
var endHour = $("#formShiftEndTime").val();
function calculate_time(formShiftStartTime, formShiftEndTime) {
if (($("#formShiftStartTime").val()) < ($("#formShiftEndTime").val()))
{
startTime = new Date('1-1-1 ' + formShiftStartTime);
endTime = new Date('1-1-1 ' + formShiftEndTime);
} else {
startTime = new Date('1-1-1 ' + formShiftStartTime);
endTime = new Date('1-2-1 ' + formShiftEndTime);
}
var difference = endTime - startTime;
difference /= 1000 * 60; // convert to minutes
var hours = Math.floor(difference / 60);
var mins = Math.floor(difference % 60);
document.getElementById('formDuration').value = showTime(hours, mins);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<form id="calculate" action="">
<div>
<p><label>Start Hour: <input type="time" name="formShiftStartTime" id="formShiftStartTime"/></label></p>
<p><label>End Hour: <input type="time" name="formShiftEndTime" id="formShiftEndTime" /></label></p>
<div class="form-group">
<label class="col-md-4 control-label">Duration</label>
<div class="col-md-8">
<input type="text" class="form-control " id="formDuration" name="formDuration">
</div>
</div>