Sounds like a job for Pipe! Like Andrea said, you can take advantage of momentJS. It is pretty awesome.
I wrote a simple pipe function that will return the difference between 2 date.
import { Pipe, PipeTransform } from '@angular/core';
import moment from 'moment'
@Pipe({
name: 'datediff'
})
export class DatediffPipe implements PipeTransform {
transform(date1: any, date2?: any): any {
const daydiff = moment(date1).diff(moment(date2), "days");
return Math.abs(daydiff);
}
}
example:
https://stackblitz.com/edit/angular-qepdnc
UPDATE:
Alternatively, if you need to keep track of the number of date that exceed 50 in your app, Pipe may not be the best tool for the job, as you will need to store state in your local component.
A possible solution reusing the same function as a method within the component class.
https://stackblitz.com/edit/angular-73xvto
this.result = this.sampleData.map(date => {
const diff = this.dateDiff(date, this.today);
return {
date,
diff
}
});
this.resultDiffCount = this.result.filter(r => r.diff > 50).length;