1

I have this code:

    function startStopwatch() {
        vm.lastTickTime = new Date();

        $interval.cancel(vm.timerPromise);
        vm.timerPromise = $interval(function() {
            var tickTime = new Date();
            var dateDiff = vm.lastTickTime.getTime() - tickTime.getTime();
            var secondsDiff = Math.abs(dateDiff / 1000);
            vm.secondsElapsed += Math.round(secondsDiff);
            vm.formattedSecondsElapsed = moment.duration(secondsElapsed, "seconds").format("HH:mm:ss");
            vm.lastTickTime = tickTime;
        }, 1000);
    }

It ticks seconds (int) since the 'play' button was hit on a stopwatch.

But the format is 01, 02.... up to 60 and then 01:01, 01:02, etc.

I'd like to format it as 00:00:00, incrementing the int. I've found answers for other programming languages but not JavaScript. Any help appreciated!

RJB
  • 2,063
  • 5
  • 29
  • 34
  • woah downvoter: this is a well thought out question with supporting code... – RJB Nov 01 '15 at 23:16
  • Why don't you build your own conversion function - it shouldn't be too hard? Not sure if something out-of-the-box exists for this purpose. Whenever I needed something like that, I was building my own in a few minutes... But it's possible there's a way to do it with moment, let's wait for an answer. – Shomz Nov 01 '15 at 23:18
  • yeah I'm hoping somebody knows a handy answer off the top of their head. I'm really confused about the downvote. I'll roll my own function and post it if I need to. – RJB Nov 01 '15 at 23:21
  • If you get no answers in, say, 10 minutes, I'll help you write your own function. Don't worry about the downvoters - there are a couple of them that randomly go and downvote everything... :/ – Shomz Nov 01 '15 at 23:22
  • 1
    I didn't downvote, but I would guess that maybe the person who did did so because your question doesn't show any attempt to fix the problem: it basically says "I wish this code did something else". You said in a comment you'd roll your own function if you need to - why not do that first? Best way to learn. Read the Moment documentation. If you get stuck, *then* ask about it in this forum. – nnnnnn Nov 01 '15 at 23:28
  • There are tons of answers, did you see them @RJB? This one looks good: http://stackoverflow.com/questions/6312993/javascript-seconds-to-time-string-with-format-hhmmss – Shomz Nov 01 '15 at 23:33
  • I was searching for a while but I didn't see that post. Thanks, solid answer. – RJB Nov 01 '15 at 23:44
  • Haha, it was the first I found. You're welcome. – Shomz Nov 01 '15 at 23:50

2 Answers2

2

Borrowing from the answer provided by RJB, convert the algorithm to a filter:

Filter

app.filter('time', function() { 
    function isInt(n){
        return Number(n) === n && n % 1 === 0;
    }
    return function(val) {
        if (isInt(val))
            return new Date(null, null, null, null, null, val).toTimeString().replace(/.*(\d{2}:\d{2}:\d{2}).*/, "$1");

        return val;
    }
});

Usage

app.controller('ctrl', function($scope) {
    $scope.seconds = 25251;
});

HTML

<div ng-controller="ctrl">
      {{ seconds | time }}
</div>

Demo Plunker

Michael Kang
  • 52,003
  • 16
  • 103
  • 135
  • Thanks for taking the question seriously. This is great; I had originally hoped to use filters. – RJB Nov 02 '15 at 00:56
1

I'm going to undelete this because I think this is the best answer and I think it has academic value.

vm.durationElapsed = new Date(null, null, null, null, null, secondsElapsed).toTimeString().replace(/.*(\d{2}:\d{2}:\d{2}).*/, "$1");

Answer found on: https://stackoverflow.com/a/12612778/1507899

Community
  • 1
  • 1
RJB
  • 2,063
  • 5
  • 29
  • 34