Calculating date difference with angular filter

前端 未结 2 1794
心在旅途
心在旅途 2021-01-14 14:34

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 ov

2条回答
  •  悲哀的现实
    2021-01-14 15:05

    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.

    {{entry.toStr | dateDiff:entry.fromStr}}

提交回复
热议问题