jquery - Calculating Military Time to Get Hours and Minutes Worked in Javascript -
i've got few day's worth of "clock in" , "clock out" entries in military (24 hour) time plain numbers.
clock in | clock out -------------------- 1020 | 1555 1116 | 1857 1049 | 1204
i've manually figured out person has worked 14 hours , 31 minutes. have html page contains lot of these entries in class, use following code them in javascript:
$('.clockin').each(function() {clockinsum += +$(this).text()||0;}); $('.clockout').each(function() {clockoutsum += +$(this).text()||0;});
i'm not sure go here, or if right way start. there way javascript/jquery calculate hours , minutes worked bunch of these entries?
you need tell timedifference.
in javascript work in milliseconds, find milliseconds difference between each start , end time, compound them , use time calculate time spent:
var list = [ ["1020", "1555"], [1116, 1857], [1049, "1204"], ]; /** * difference between 2 times in milliseconds * * @param {(number | string)} start * @param {(number | string)} end * @returns {number} */ function gettimedifference(start, end) { var d1 = new date(0); d1.sethours(parseint(start.tostring().substr(0, 2), 10)); d1.setminutes(parseint(start.tostring().substr(2, 2), 10)); var d2 = new date(0); d2.sethours(parseint(end.tostring().substr(0, 2), 10)); d2.setminutes(parseint(end.tostring().substr(2, 2), 10)); return d2.gettime() - d1.gettime(); } //figure how long guy has worked: var compiledtime = 0; (var index = 0; index < list.length; index++) { compiledtime += gettimedifference(list[index][0], list[index][1]); } //at point "compiledtime" contains milliseconds guy has worked //let's print nice , pretty var compiledtimedate = new date(compiledtime); alert("hours: " + compiledtimedate.gethours() + "\n" + "minutes: " + compiledtimedate.getminutes() + "\n" + "seconds: " + compiledtimedate.getseconds() + "\n" + compiledtimedate.gethours() + ':' + compiledtimedate.getminutes() + ':' + compiledtimedate.getseconds()); //from here can use date object methods whatever
Comments
Post a Comment