如何在angularjs中人性化持续时间?

时间:2015-11-06 07:18:16

标签: javascript jquery angularjs momentjs ngtable

我正在使用momentjsmoment-duration-formatY M D HH:mm:ss格式显示持续时间,具体取决于持续时间,如果它包括年,月,日,小时,分钟和秒,在此字段上使用ngTable排序和过滤。

代码

  $scope.getTableDataDuration = function(item) {
      if (angular.isDefined(item) && item != null) {
          var tmp = "";
          if (item > 31536000) { //year
              tmp = moment.duration(item, "year").format("Y M D h:mm:s");
              return tmp;
          } else if (item > 2628000) { //month
              tmp = moment.duration(item, "month").format("M D h:mm:s");
              return tmp;
          } else if (item >= 86400) { //day
              tmp = moment.duration(item, "days").format("D h:mm:s");
              return tmp;
          } else if (item < 60) {
              return item;
          } else if (item < 3600) { //minute
              tmp = moment.duration(item, "minutes").format("h:mm:s");
              return tmp;
          }
      }
      return;
  }

Plunker link

更新

例如:

duration in seconds 331 should show 05:51
duration in seconds 7245 should show 02:01:25 
similarly till years

考虑到持续时间,我需要将其人性化,类似于上面的例子

1 个答案:

答案 0 :(得分:2)

我相信你误解了文档。我没有亲自使用moment-duration,但是从GitHub上的文档来看,你想要使用的是这个

  moment.duration(99544, "seconds").format("D h:mm:s");

它通知持续时间插件,输入数字以秒为单位。然后通过提供的格式字符串对其进行格式化。

请参阅此示例https://github.com/jsmreese/moment-duration-format#weeks

在这里,他使用moment.duration(123, "days").format("w W", 2);基本上说&#34;我有123天,我想把它转换为周数&#34;

相关问题