2

In input, I have a number of seconds. In output, I want minutes and seconds, on two digits, separated by :

e.g.

  • 1 -> 00:01
  • 72 -> 01:12

I come up with the following code :

formatTime: function(nbSeconds) {
  var min = Math.ceil(nbSeconds/60);
  var sec = nbSeconds % 60;
  var formattedMin = min.toLocaleString('fr-FR', { minimumIntegerDigits: 2 });
  var formattedSec = sec.toLocaleString('fr-FR', { minimumIntegerDigits: 2 });
  return formattedMin + ":" + formattedSec;
},

Is there a better way ?

Yang
  • 8,580
  • 8
  • 33
  • 58
Yann Moisan
  • 8,161
  • 8
  • 47
  • 91
  • 3
    Passing 1 returned me `01:01`, use `Math.floor` instead of `Math.ceil` to get minutes, is that **better**? – Tushar Oct 10 '15 at 09:16
  • try this `var formatTime = function(ticks) { var hh = Math.floor( ticks / 3600); var mm = Math.floor( (ticks % 3600) / 60); var ss = (ticks % 3600) % 60; return hh + ":" + mm + ":" + ss; };` – Kishore Sahasranaman Oct 10 '15 at 09:20
  • It seems that your code currently works, and you are looking to improve it. Generally these questions are too opinionated for this site but you might find better luck at [CodeReview.SE](http://codereview.stackexchange.com/tour). Remember to read [their question requirements](http://codereview.stackexchange.com/help/on-topic) as they are more strict than this site. – Kyll Oct 10 '15 at 09:28
  • 1
    Looks like a good fit for Code Review, though I wouldn't say our requirements are stricter, just different ^^ – Kaz Oct 10 '15 at 09:30
  • Check my answer: http://stackoverflow.com/a/33054094/4365315. If you have any problem, let me know – Sherali Turdiyev Oct 10 '15 at 12:41

0 Answers0