使用角度过滤器计算日期差异 [英] Calculating date difference with angular filter

查看:35
本文介绍了使用角度过滤器计算日期差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够计算两天之间的差异(包括两天)并显示差异.理想情况下,这将通过角度过滤器进行,因此它可以在整个应用程序中使用.

解决方案

JS 过滤器

generalFilters.filter('dateDiff', function () {var magicNumber = (1000 * 60 * 60 * 24);返回函数(toDate,fromDate){if(toDate && fromDate){var dayDiff = Math.floor((toDate - fromDate)/magicNumber);如果(angular.isNumber(dayDiff)){返回 dayDiff + 1;}}};});

HTML 来显示值.

{{entry.toStr |dateDiff:entry.fromStr}} <ng-pluralize count="entry.toStr | dateDiff:entry.fromStr" when="{1:'Day', other: 'Days'}"></ng-pluralize></div>

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 Filter

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 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>

这篇关于使用角度过滤器计算日期差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆