2

I'd like my countdown timer to show double digits, like "53:00" instead of "53:0". Not sure what's going wrong here? Thank you!

function convertSeconds(s) {
    var min = Math.floor(s / 60);
    var sec = s % 60;
    return nf(min, 2) + ':' + nf(sec, 2);
  }
5
  • 6
    Well that depends on whatever nf() is - you need to add that code to your question. Commented Mar 25, 2019 at 16:14
  • Thought that was numberformat? Do I need to make it its own function and refer to that with nf? What extra code do you expect to see? Srry, new to coding. Thanks for your time! Commented Mar 25, 2019 at 16:17
  • If your code works without errors then there is an nf() function written somewhere - in a library you have included for example, its not a built in part of JavaScript. Commented Mar 25, 2019 at 16:19
  • nf is not a built-in Javascript function, you will need to show where it's defined, otherwise it's impossible to tell what it is and what it does. Commented Mar 25, 2019 at 16:19
  • Possible duplicate of How to format numbers by prepending 0 to single-digit numbers? Commented Mar 25, 2019 at 16:23

4 Answers 4

2

You can use padStart() to make sure a string is a certain length. If it's not it will pad it with whatever you want. In this case 0:

const nf = (n, c) => n.toString().padStart(c, '0');

function convertSeconds(s) {
  var min = Math.floor(s / 60);
  var sec = (s % 60)
  return nf(min, 2) + ':' + nf(sec, 2);
}

console.log(convertSeconds(64))
console.log(convertSeconds(119))

Not sure if you want to pad the minutes of not.

There is also the Intl.NumberFormat() which has an option for minimum digits (though this seems like overkill for this):

console.log(new Intl.NumberFormat('en-US', { minimumIntegerDigits: 2}).format(2));

console.log(new Intl.NumberFormat('en-US', { minimumIntegerDigits: 2}).format(59));

Sign up to request clarification or add additional context in comments.

Comments

2

To pad zeroes you can do

function nf(num, places) {
  var s = '0' + num;
  return s.slice(places * -1);
}

function convertSeconds(s) {
  var min = Math.floor(s / 60);
  var sec = s % 60;
  return nf(min, 2) + ':' + nf(sec, 2);
}

Should get you what you want

1 Comment

There must be an nf() somewhere in your code stack, I would the code above with a different name as nf()'s current behavior/dependencies are unknown.
1

If by nf() you mean a function that formats a Number as string of given digits, this can be an implementation of it:

function nf(number, digits) {
  var res = number.toString();
  while (res.length < digits) {
    res = "0" + res;
  }
  return res;
}

Demo:

function convertSeconds(s) {
  var min = Math.floor(s / 60);
  var sec = s % 60;
  return nf(min, 2) + ':' + nf(sec, 2);
}

function nf(number, digits) {
  var res = number.toString();
  while (res.length < digits) {
    res = "0" + res;
  }
  return res;
}

console.log(nf(4,2));

Comments

1

The nf() function should be like this:

function nf(num){
    if(num < 10){
        return "0"+num;
    }
    return num;
}

console.log(nf(7));
// 07
console.log(nf(11));
// 11

1 Comment

You don't need to convert to a string for the condition. return n <= 9 ? '0' + n : n; will be a slightly shorter way…

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.