Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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>
like image 918
Matan Yadaev Avatar asked Feb 08 '15 13:02

Matan Yadaev


People also ask

How do you convert seconds to HH MM SS?

Find the number of whole hours by dividing the number of seconds by 3,600. The number to the left of the decimal point is the number of whole hours. The number to the right of the decimal point is the number of partial hours. Then, convert the remaining partial hours to minutes by multiplying the partial hours by 60.

What is the correct way to apply filter in AngularJS?

In AngularJS, you can also inject the $filter service within the controller and can use it with the following syntax for the filter. Syntax: $filter("filter")(array, expression, compare, propertyKey) function myCtrl($scope, $filter) { $scope. finalResult = $filter("filter")( $scope.

What is $scope in AngularJS?

$rootScope is a parent object of all “$scope” angular objects created in a webpage. $scope is a child object that is used to bind the HTML(view) & Javascript(Controller) in a webpage. It is created with the ng-app directive. It is created with the ng-controller directive.

What is $filter in AngularJS?

Filters are used for formatting data displayed to the user. They can be used in view templates, controllers or services. AngularJS comes with a collection of built-in filters, but it is easy to define your own as well.


3 Answers

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

like image 132
manzapanza Avatar answered Sep 21 '22 16:09

manzapanza


manzapanza's answer only works if the seconds are less than 86400 (1 day). The date object needs to be completely zero. Also, it would be better to return the actual date object so that angularjs does not have to make it again.

app.filter('secondsToDateTime', function() {     return function(seconds) {         var d = new Date(0,0,0,0,0,0,0);         d.setSeconds(seconds);         return d;     }; }); 

and

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

Edit: If you want hours to go above 24 without wrapping to days it is better to not use Date:

app.filter('secondsToTime', function() {      function padTime(t) {         return t < 10 ? "0"+t : t;     }      return function(_seconds) {         if (typeof _seconds !== "number" || _seconds < 0)             return "00:00:00";          var hours = Math.floor(_seconds / 3600),             minutes = Math.floor((_seconds % 3600) / 60),             seconds = Math.floor(_seconds % 60);          return padTime(hours) + ":" + padTime(minutes) + ":" + padTime(seconds);     }; }); 

and

<b>{{seconds | secondsToTime}}</b> 
like image 36
makman99 Avatar answered Sep 19 '22 16:09

makman99


Try this:

app.filter('secondsToHHmmss', function($filter) {
    return function(seconds) {
        return $filter('date')(new Date(0, 0, 0).setSeconds(seconds), 'HH:mm:ss');
    };
})

html:

<b>{{seconds | secondsToHHmmss}}</b>
like image 29
endru Avatar answered Sep 18 '22 16:09

endru