具有角滤波运算时间差时间差、有角

2023-09-13 04:02:30 作者:坠 梦。

我需要能够计算两天(含)之间的差异,并显示差。理想情况下,这将是经由一个角滤波器,因此它可以遍布该应用程序中使用。

I needed to be able to calculate the difference between two days, inclusive, and display the difference. Ideally this would be via an angular filter so it can be used all over the application.

推荐答案

JS过滤器

generalFilters.filter('dateDiff', function () {
  var magicNumber = (1000 * 60 * 60 * 24);

  return function (toDate, fromDate) {
    if(toDate && fromDate){
      var dayDiff = Math.floor((toDate - fromDate) / magicNumber);
      if (angular.isNumber(dayDiff)){
        return dayDiff + 1;
      }
    }
  };
});

HTML显示值。

HTML to display the value.

<div class="field-value">{{entry.toStr | dateDiff:entry.fromStr}} <ng-pluralize count="entry.toStr | dateDiff:entry.fromStr" when="{1:'Day', other: 'Days'}"></ng-pluralize></div>
 
精彩推荐
图片推荐