Angularjs秒到时间过滤

时间:2015-02-08 13:41:42

标签: angularjs date time

我正在使用Angularjs。

我有一串数字,例如:237。这意味着秒。我需要将其更改为:2:343:42:54

我需要几秒钟的时间过滤。我该怎么做?

感谢。

6 个答案:

答案 0 :(得分:72)

尝试这样的事情:

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

答案 1 :(得分:32)

manzapanza的回答仅在秒数小于86400(1天)时有效。日期对象需要完全为零。此外,最好返回实际的日期对象,以便angularjs不必再次进行。

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

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

修改:如果你想要数小时超过24小时而不包装到几天,最好不要使用日期:

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);
    };
});

<b>{{seconds | secondsToTime}}</b>

答案 2 :(得分:8)

试试这个:

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>

答案 3 :(得分:5)

a = raw_input("First word please?")
b = raw_input("Second word please?")

def scramble(word1, word2):

    if len(word1) > len(word2):
        a1 = word1
        a2 = word2
    else:
        a1 = word2
        a2 = word1

    maxlength = max( len(word1),len(word2) )
    magicWord = ""

    for x in range(0,maxlength):

        magicWord += a1[x]

        if x < len(a2)-1:
            magicWord += a2[x]

        elif x == len(a1)-1:
            fin = len(a2)-1
            magicWord += a2[fin]

        else:
            magicWord += "0"

    return magicWord

magicWord = scramble(a,b)
print "\nYour magic word is: \""+magicWord+"\"\n"

magicSize = len(magicWord)



def unscramble(magicWord):

    z = 0

    if z == magicSize:
        return False

    else:
        while unscramble(magicWord):

            word1 = ""
            word2 = ""

            if z % 2 == 1:
                if magicWord[z] == "0":
                    z+= 1
                else:
                    word2 += magicWord[z]
                    z += 1
            else:
                if magicWord[z] == "0":
                    z+= 1
                else:
                    word1 += magicWord[z]
                    z += 1

            return word1

if word1 > word2:
    print "First input: " + word1
    print "Second input: " + word2
else:
    print "First input: " + word2
    print "Second input: " + word1

将输出:02:04:14

答案 4 :(得分:2)

我认为没有必要使用自定义过滤器

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

function Scoper($scope) {
    $scope.seconds = '79000';
    $scope.hours = parseInt($scope.seconds / 3600);
}

答案 5 :(得分:0)

by @manzapanza解决方案几天:

  $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);
  }

在视图中:

{{duration_for(100500)}}