0

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);

3
  • Well how can it be greater than 60? Commented Mar 2, 2016 at 14:07
  • @epascarello 53 seconds plus 25 are 78 seconds. Commented Mar 2, 2016 at 14:09
  • @reporter Yeah but dfbsec = dfbsec % 60; nullifies your argument. Commented Mar 2, 2016 at 14:09

2 Answers 2

2

It's your order of operation. You're cutting the value off before checking how many minutes to add.

  dfbmin = parseInt(splitdfbrun[0]) + parseInt(splitdfbpen[0])
  dfbmin = dfbmin % 60;

  dfbsec = parseInt(splitdfbrun[1]) + parseInt(splitdfbpen[1])

  // Update Minutes
  if (dfbsec >= 60) {
    dfbmin = dfbmin + 1;
  }

  // Update seconds
  dfbsec = dfbsec % 60;

  if (dfbsec < 10) {
    dfbsec = '0' + dfbsec;
  }
Sign up to request clarification or add additional context in comments.

1 Comment

Ok, I see it now. I was going crazy there for a little while trying to figure out what the heck I was doing wrong. Haha. When it was just a simple order of operation. Thank you for that.
1

You can convert all to seconds and back to minutes and seconds. That makes it easier to grasp the order.

function pad(num) {
  return ("0"+num).slice(-2);
}
function getTot(dfbrun,dfbpen) {
  var splitdfbrun = dfbrun.split(':');
  var splitdfbpen = dfbpen.split(':');

  var dfbmin;
  var dfbsec;

  <!-- DFB Time input -->

  dfbmin = parseInt(splitdfbrun[0],10) + parseInt(splitdfbpen[0],10);
  dfbsec = dfbmin * 60;

  dfbsec += parseInt(splitdfbrun[1],10) + parseInt(splitdfbpen[1],10);

  dfbmin = Math.floor(dfbsec / 60);
  dfbsec = dfbsec - dfbmin * 60;

  return  pad(dfbmin) + ':' + pad(dfbsec);
}

var dfbrun = "01:53"
var dfbpen = "00:25"
  
document.write(getTot(dfbrun,dfbpen));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.