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

后端 未结 6 2019
粉色の甜心
粉色の甜心 2020-12-15 17:34

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 shou

相关标签:
6条回答
  • 2020-12-15 18:05

    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>
    
    0 讨论(0)
  • 2020-12-15 18:05

    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

    0 讨论(0)
  • 2020-12-15 18:22

    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>
    
    0 讨论(0)
  • 2020-12-15 18:22

    I think it is not necessary the custom filter

    <b>{{hours}}{{seconds | date:':mm:ss'}}</b>
    
    function Scoper($scope) {
        $scope.seconds = '79000';
        $scope.hours = parseInt($scope.seconds / 3600);
    }
    
    0 讨论(0)
  • 2020-12-15 18:24
    app.filter('formatTimer', function () {
    return function (input) {
        function z(n) { return (n < 10 ? '0' : '') + n; }
        var seconds = input % 60;
        var minutes = Math.floor(input % 3600 / 60);
        var hours = Math.floor(input / 3600);
        return (z(hours) + ':' + z(minutes) + ':' + z(seconds));
    };
    

    will output: 02:04:14

    0 讨论(0)
  • 2020-12-15 18:25

    by @manzapanza solution for days:

      $scope.duration_for = function(seconds){
        if(!seconds) return
        var duration = new Date(1970, 0, 1).setSeconds(seconds)
    
        var mask = 'HH:mm'
        if(seconds >= 86400){
          mask = 'd days,' + mask
        }
    
        return $filter('date')(duration, mask);
      }
    

    in view:

    {{duration_for(100500)}}
    
    0 讨论(0)
提交回复
热议问题