在字符串时间中添加20分钟并将其填充到文本框中或提醒它

我在select / list控件中填充了一个时间字符串,例如05:40我想在其中添加20分钟,并且每当列表值在jquery或javascript中获得更改时填充在文本框中

$(document).ready(function() { $("#starttimeList").change(function() { var selectVal = $('#starttimeList').val(); ////// HERE I WANT MY CODE AS selectVal is getting value of select control eg 05:40 and entTime is my textbox where I want the time added with 20 minutes. $("#endTime").val(selectVal); }); }); 

如果时间格式为hh:mm,则可以使用简单函数将其转换为分钟,添加更多分钟,然后再将其转换回hh:mm:

 function addMinutes(time/*"hh:mm"*/, minsToAdd/*"N"*/) { function z(n){ return (n<10? '0':'') + n; } var bits = time.split(':'); var mins = bits[0]*60 + (+bits[1]) + (+minsToAdd); return z(mins%(24*60)/60 | 0) + ':' + z(mins%60); } addMinutes('05:40', '20'); // '06:00' addMinutes('23:50', 20); // '00:10' addMinutes('00:00', '120'); // '02:00' 

要添加的分钟数可以是数字或字符串。

如果您需要允许夏令时,使用日期对象可以使生活更轻松。