How to Add Hours to Selected Time in javascript -
How to Add Hours to Selected Time in javascript -
i'm pretty new javascript , jquery, have 3 fields fromtime, working hrs , time. if select time 10:00 , working hrs 03:00 expected result 02:00 time.
<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>
only add together hrs selected time.
you don't need third-party js library accomplish want. since not giving farther information, assume that:
times internally represented in armed forces time (i.e 10:00 pm = 22:00). example, there item like:<option value="22:00">10:00 pm</option>
. maximum representable time 23:59. the value of "working hours" cannot exceed 24 hours. the next code returns armed forces time of adding given time amount (in hours) given armed forces time:
function addhrs(time, toadd) { var hh = parseint(time.substr(0, 2), 10); //get hours var mm = parseint(time.substr(3, 2), 10); //get minutes var ahh = parseint(toadd.substr(0, 2), 10); //get hours add together var amm = parseint(toadd.substr(3, 2), 10); //get minutes add together 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 exceed minutes in day. var result_hours = math.floor(result_minutes / 60); result_minutes -= result_hours * 60; homecoming ('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
javascript
Comments
Post a Comment