Mat*_*yev 24 time date angularjs
我正在使用Angularjs.
我有一串数字,例如:610.这意味着秒.我需要它改为:10:10或者610.
我需要几秒钟的时间过滤.我该怎么做?
谢谢.
man*_*nza 72
尝试这样的事情:
app.filter('secondsToDateTime', [function() {
return function(seconds) {
return new Date(1970, 0, 1).setSeconds(seconds);
};
}])
Run Code Online (Sandbox Code Playgroud)
HTML:
<b>{{seconds | secondsToDateTime | date:'HH:mm:ss'}}</b>
Run Code Online (Sandbox Code Playgroud)
mak*_*n99 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;
};
});
Run Code Online (Sandbox Code Playgroud)
和
<b>{{seconds | secondsToDateTime | date:'HH:mm:ss'}}</b>
Run Code Online (Sandbox Code Playgroud)
编辑:如果你想要数小时超过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);
};
});
Run Code Online (Sandbox Code Playgroud)
和
<b>{{seconds | secondsToTime}}</b>
Run Code Online (Sandbox Code Playgroud)
试试这个:
app.filter('secondsToHHmmss', function($filter) {
return function(seconds) {
return $filter('date')(new Date(0, 0, 0).setSeconds(seconds), 'HH:mm:ss');
};
})
Run Code Online (Sandbox Code Playgroud)
HTML:
<b>{{seconds | secondsToHHmmss}}</b>
Run Code Online (Sandbox Code Playgroud)
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));
};
Run Code Online (Sandbox Code Playgroud)
将输出:02:04:14