具有角滤波运算时间差 [英] Calculating date difference with angular filter

查看:109
本文介绍了具有角滤波运算时间差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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>

这篇关于具有角滤波运算时间差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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