0

I have a form that takes inputted information and displays that information on another page. The way that the form is set up is the user enters in a run time, which is separated off with a colon, and enters a penalty time in the same format. The way that the data is being processed and shown is without any leading zeros. For example, for the run time, the user enters in 01:00 and penalty time 00:05. The way this information is being outputted is 1:5, and the way that I want the information to be outputted is 01:05. I know that there is not javascript function that always for retaining leading zeros, but I cannot seem to figure out how to accomplish this.

This is the code that I have.

<script type = "text/javascript">
function calculate()
{


<!-- Variables -->
var dfbrun = document.getElementById("dfb_run").value;
var dfbpen = document.getElementById("dfb_pen").value;
var splitdfbrun = dfbrun.split(':');
var splitdfbpen = dfbpen.split(':');
var dfbmin;
var dfbsec;
var dfbtot;

<!-- DFB Time input -->
dfbmin = parseFloat(splitdfbrun[0].slice(-4)) +    parseFloat(splitdfbpen[0].slice(-4))
dfbmin = dfbmin%60;

dfbsec = parseInt(splitdfbrun[1].slice(-2)) + parseInt(splitdfbpen[1].slice(-2))
dfbsec = dfbsec%60;

alert(+dfbmin+ ':' +dfbsec)
dfbtot = +dfbmin+ ':' +dfbsec;
document.getElementById("dfb_com").value = dfbtot;
}

jQuery(function ($) {
         $.mask.definitions['~']='[+-]';
        $('.run').mask('99:99');
        $('.penalty').mask('99:99');

    })
</script>

<form method = "GET" action = "RodeoDisplay.php" target = "RodeoDisplay.php">
<table>
<tr> 
    <td> Site: <input type = "text" value = "DFB" readonly = "true" size = "4"> </td>
    <td> Run Time: <input type = "text" size = "5" id = "dfb_run" class = "run" name = "dfb_run">
    <td> Penalty: <input type = "text" class = "penalty" size = "5" id = "dfb_pen"> </td>
    <td> Completed Time: <input type = "text" readonly = "true" size = "5" id = "dfb_com"> </td>
</tr>
</table>

Would I also be better off using the time function?

novicevba
  • 81
  • 10
  • convert to strings, make a 'pad' utility function to pad the front with the amount of '0's you need. the key it to keep them strings at this point. – rlemon Feb 29 '16 at 21:52
  • 2
    A number can't have a leading zero, simply because `01` is really just `1`. If you want leading zeros you'd convert to strings by zero-padding, and there are hundreds of answers on how to do that on this site. – adeneo Feb 29 '16 at 21:53
  • 1
    http://stackoverflow.com/questions/1267283/how-can-i-create-a-zerofilled-value-using-javascript – adeneo Feb 29 '16 at 21:54

0 Answers0