AngularJS - 如何将毫秒转换为x小时y分钟

19

我有一个需求,需要在AngularJS中将毫秒转换为x小时y分钟。

例如,3600000应该显示为1小时0分钟。

我尝试使用Angular网站上日期格式中的date:'H:m'date:'HH:mm'

但是它们给出的是19:0和19:00,而不是1小时0分钟。

请问如何实现这个功能?

谢谢。


3
你可以编写自定义筛选器。date不能用于显示时间间隔。 - akonsu
9个回答

31

让我们为此创建一个自定义过滤器,例如:

.filter('millSecondsToTimeString', function() {
  return function(millseconds) {
    var oneSecond = 1000;
    var oneMinute = oneSecond * 60;
    var oneHour = oneMinute * 60;
    var oneDay = oneHour * 24;

    var seconds = Math.floor((millseconds % oneMinute) / oneSecond);
    var minutes = Math.floor((millseconds % oneHour) / oneMinute);
    var hours = Math.floor((millseconds % oneDay) / oneHour);
    var days = Math.floor(millseconds / oneDay);

    var timeString = '';
    if (days !== 0) {
        timeString += (days !== 1) ? (days + ' days ') : (days + ' day ');
    }
    if (hours !== 0) {
        timeString += (hours !== 1) ? (hours + ' hours ') : (hours + ' hour ');
    }
    if (minutes !== 0) {
        timeString += (minutes !== 1) ? (minutes + ' minutes ') : (minutes + ' minute ');
    }
    if (seconds !== 0 || millseconds < 1000) {
        timeString += (seconds !== 1) ? (seconds + ' seconds ') : (seconds + ' second ');
    }

    return timeString;
};
});
然后使用它:
<div>{{ millSeconds | millSecondsToTimeString }}</div>

11

在AngularJS中有日期转换器,只需设置所需的日期格式:

{{milliseconds | date:'yyyy-MM-dd HH:mm'}}

我也使用jQuery的timeago()函数创建了这样的“timeAgo”过滤器:

.filter('timeAgo', function() {
    return function(input) {
        if (input == null) return "";
        return jQuery.timeago(input);
    };
})

用法:

{{milliseconds | timeAgo}}

或者同时使用两种格式来表示较宽的日期:

<span>{{milliseconds | timeAgo}}, {{milliseconds  | date:'yyyy-MM-dd HH:mm'}}</span>

结果:

12 minutes ago, 2015-03-04 11:38

10

您需要使用moment的持续时间对象。

要实现您想要的功能,请尝试以下操作:

app.controller 'MainCtrl', ($scope) ->
    $scope.name = 'World'
    $scope.milliseconds = 3600000
    $scope.duration = moment.duration($scope.milliseconds)

和标记

<body ng-controller="MainCtrl">
  <p>Milliseconds: {{milliseconds}}</p>
  <p>Duration: {{duration.hours()}}h {{duration.minutes()}}m</p>
</body>

plunkr: http://plnkr.co/edit/2HL75Tmr4CoAy5R9smkx

这是一个关于编程的网站链接,可以通过该链接访问相关内容。


5

谢谢。我稍微修改了你的过滤器,使其返回以hh:mm:ss格式表示的持续时间。

.filter('duration', function() {
    //Returns duration from milliseconds in hh:mm:ss format.
      return function(millseconds) {
        var seconds = Math.floor(millseconds / 1000);
        var h = 3600;
        var m = 60;
        var hours = Math.floor(seconds/h);
        var minutes = Math.floor( (seconds % h)/m );
        var scnds = Math.floor( (seconds % m) );
        var timeString = '';
        if(scnds < 10) scnds = "0"+scnds;
        if(hours < 10) hours = "0"+hours;
        if(minutes < 10) minutes = "0"+minutes;
        timeString = hours +":"+ minutes +":"+scnds;
        return timeString;
    }
});

0
(function () {
    'use strict';

    angular
        .module('module_name')
        .filter('secondsToDateTime', secondsToDateTime);

    function secondsToDateTime() {
        return function(seconds) {
            return new Date(1970, 0, 1).setSeconds(seconds);
        };
    }
})();


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

0

试试这个

var app = angular.module ( 'myApp', [] ) ;
app.controller ( 'myController', function ( $scope, $filter) {
    $scope.date = $filter('date')(milliseconds, 'MM/dd/yyyy');
});

虽然这段代码可能回答了问题,但提供关于为什么和/或如何回答问题的额外上下文可以提高其长期价值。 - Benjamin W.

0

对于希望使用逗号分隔符(例如“21天,14小时,7分钟”)的任何人:

'use strict';

angular.module('contests').filter('duration', function () {
    return function(milliseconds) {
        var seconds = Math.floor(milliseconds / 1000);
        var days = Math.floor(seconds / 86400);
        var hours = Math.floor((seconds % 86400) / 3600);
        var minutes = Math.floor(((seconds % 86400) % 3600) / 60);
        var dateTimeDurationString = '';
        if ((days > 0) && (hours === 0 && minutes === 0)) dateTimeDurationString += (days > 1) ? (days + ' days ') : (days + ' day ');
        if ((days > 0) && (hours > 0 || minutes > 0)) dateTimeDurationString += (days > 1) ? (days + ' days, ') : (days + ' day, ');
        if ((hours > 0) && (minutes > 0)) dateTimeDurationString += (hours > 1) ? (hours + ' hours, ') : (hours + ' hour, ');
        if ((hours > 0) && (minutes === 0)) dateTimeDurationString += (hours > 1) ? (hours + ' hours ') : (hours + ' hour ');
        if (minutes > 0) dateTimeDurationString += (minutes > 1) ? (minutes + ' minutes ') : (minutes + ' minute ');
        return dateTimeDurationString;
    };
});

0

适用于TypeScript,但可以调整为任何语言。

convertTimeMS(timeMS: number): string {
  const seconds = Math.floor(timeMS / 1000);
  const minutes = Math.floor(seconds / 60);
  const hours = Math.floor(minutes / 60);

  let str: string = '';

  if(hours > 0) {
    str = str + hours.toString() + 'h ';
  }
  if(minutes%60 > 0) {
    str = str + Number(minutes%60).toString() + 'm ';
  }
  if(seconds%60 > 0) {
    str = str + Number(seconds%60).toString() + 's ';
  }

  return str;
}

0
请使用以下语法。
$filter('date')(dateObj, format)

例如

$filter('date')(1324339200000, 'dd/MM/yyyy');

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接