Calculating date difference with angular filter
I needed to calculate the difference between two days inclusive and show the difference. Ideally this will be through an angular filter so that it can be used throughout the application.
+3
zmanc
source
to share
2 answers
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>
+6
zmanc
source
to share
Duplicate 26649194
angular-moment does the trick! ... and (very) even more.
Using amDifference filter :
Get the difference between two dates in milliseconds. Parameters: date, units and usePrecision. The default date is the current date. Example:
<span>Difference: {{ dateFrom | amDifference : dateTo : 'days' }} days</span>
+2
s1moner3d
source
to share