I have a javascript function that I am creating. It is take a time (mm:ss) inputted by a user and then being displayed on a php page.
One of the problems that I, for some reason, cannot seem to work through is getting the minutes to increase by x when seconds is greater than 60.
For example, if the user enters in a run time of 01:53 and a penalty time of 00:25, the output is 01:18, when it really should be 02:18.
I created an if statement with the condition being if seconds is great than 60, to increment the minute by 1.
This is the function that I have so far. Also, is it easier to handle the input of time this way, or would it be easier and more efficient to handle time using the time() function?
function dfbcalc() {
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 = parseInt(splitdfbrun[0]) + parseInt(splitdfbpen[0])
dfbmin = dfbmin % 60;
dfbsec = parseInt(splitdfbrun[1]) + parseInt(splitdfbpen[1])
dfbsec = dfbsec % 60;
if (dfbsec < 10) {
dfbsec = '0' + dfbsec;
}
if (dfbsec > 60) {
dfbmin = dfbmin + 1;
}
alert(+dfbmin + ':' + dfbsec)
dfbtot = '0' + dfbmin + ':' + dfbsec;
document.getElementById("dfb_com").value = dfbtot;
}
var dfbrun = "01:53"
var dfbpen = "00:25"
var splitdfbrun = dfbrun.split(':');
var splitdfbpen = dfbpen.split(':');
var dfbmin;
var dfbsec;
var dfbtot;
<!-- DFB Time input -->
dfbmin = parseInt(splitdfbrun[0]) + parseInt(splitdfbpen[0])
dfbmin = dfbmin % 60;
dfbsec = parseInt(splitdfbrun[1]) + parseInt(splitdfbpen[1])
dfbsec = dfbsec % 60;
if (dfbsec < 10) {
dfbsec = '0' + dfbsec;
}
if (dfbsec > 60) {
dfbmin = dfbmin + 1;
}
document.write(+dfbmin + ':' + dfbsec+"<br/>")
dfbtot = '0' + dfbmin + ':' + dfbsec;
document.write(dfbtot);
dfbsec = dfbsec % 60;nullifies your argument.