Angular.js: Seconds to HH:mm:ss filter

Matan Yadaev picture Matan Yadaev · Feb 8, 2015 · Viewed 46.9k times · Source

I have a number of seconds count, for example, 713 seconds. How can I implement an Angular.js filter that converts this 713 seconds to HH:mm:ss format? In this case, it should be 00:11:53

<div>
  {{ 713 | secondsToHHMMSS }} <!-- Should output 00:11:53 -->
</div>

Answer

manzapanza picture manzapanza · Feb 9, 2015

Try something like this:

app.filter('secondsToDateTime', [function() {
    return function(seconds) {
        return new Date(1970, 0, 1).setSeconds(seconds);
    };
}])

html:

<b>{{seconds | secondsToDateTime | date:'HH:mm:ss'}}</b>

Demo