我是javascript和jQuery的新手,我有三个字段FromTime,Working hrs和To Time。如果我从时间10:00和工作时间03:00选择,那么预期结果是02:00进入时间。
<select name="timestart">
<option value="00:00">00:00 am</option>
<option value="00:30">00:30 am</option>
<option value="01:00">01:00 am</option>
</select>
仅在选定时间添加小时。
答案 0 :(得分:0)
您不需要任何第三方JS库来实现您的目标。由于您没有提供进一步的信息,我将假设:
<option value="22:00">10:00 pm</option>
。最长可表示时间为23:59。以下代码返回将给定时间量(以小时为单位)添加到给定军事时间的军事时间:
function addhrs(time, toadd) {
var hh = parseInt(time.substr(0, 2), 10); //Get the hours
var mm = parseInt(time.substr(3, 2), 10); //Get the minutes
var ahh = parseInt(toadd.substr(0, 2), 10); //Get the hours to add
var amm = parseInt(toadd.substr(3, 2), 10); //Get the minutes to add
var minutes = hh*60 + mm;
var minutes_to_add = ahh*60 + amm;
var result_minutes = (minutes + minutes_to_add) % 1440; //Prevent total number of minutes to exceed the minutes in a day.
var result_hours = Math.floor(result_minutes / 60);
result_minutes -= result_hours * 60;
return ('0'+result_hours).substr(-2) + ":" + ('0'+result_minutes).substr(-2);
}
addhrs('22:00', '03:00'); //Returns: 01:00
addhrs('12:30', '01:30'); //Returns 14:00
addhrs('12:00', '24:00'); //Returns 12:00
addhrs('12:00', '12:00'); //Returns 00:00