Hours and Minutes Calculation
24 May 2006
Calculating time like hours and minutes in Javascript for some reasons is not as easy as any other programming or script languages. Here you can find some Javascript samples of hours and minutes calculation functions that might be useful for calculating engine running hours or flight hours.
This function using 24 hours format of hours and minutes without calculating the seconds.
The Scripts
You can view or copy the scripts below and Paste inside the HEAD tag area in your HTML document.
<script type="text/javascript"><!--function hourminutecalc(id_of_result, id_of_operand1, id_of_operand2){var time1;var time2;var h1;var m1;var h2;var m2;var timeresult;var myhours;var myminutes;var hour_div;var minute_div;time1 = document.getElementById(id_of_operand1);time2 = document.getElementById(id_of_operand2);h1 = time1.value.substr(0,2);m1 = time1.value.substr(3,2);h1 = Number(h1)*60;m1 = Number(m1);h2 = time2.value.substr(0,2);m2 = time2.value.substr(3,2);h2 = Number(h2)*60;m2 = Number(m2);timeresult = (h2+m2)-(h1+m1);myhours = parseInt(timeresult/60);myminutes = timeresult - (myhours*60);if(myhours < 10){myhours = '0' + myhours;};if(myminutes < 10){myminutes = '0' + myminutes;};document.getElementById(id_of_result).value = myhours + ':' + myminutes;}--></script>