比较两个日期与管道角度

时间:2018-07-12 16:03:30

标签: javascript angular date pipe

我使用Angular CLI 6,angularfire2和firebase。

我想比较两个日期:StartDate和EndDate。

用英语,如果开始日期的月份和年份= EndDate的月份和年份,则不显示结束日期。

我尝试这个:

component.html:

<mat-list *ngIf="compareDates()">({{ ppsToDisplay?.datefin | date:'MMMM y'}})
Component.ts

compareDates(): boolean {
   return this.datePipe.transform(this.ppssToDisplay.datefin, 'MM-yyyy') === 
            this.datePipe.transform(this.ppssToDisplay.datedebut, 'MM-yyyy');
}

在这里,我无法编写打字稿来考虑管道。谢谢您的帮助

1 个答案:

答案 0 :(得分:1)

您可以如下编写您的compareDates

sameDates(): boolean {
   return datePipe.transform(this.StartDate, 'MM-yyyy') === 
            datePipe.transform(this.EndDate, 'MM-yyyy');
}

您将必须在组件构造函数中注入DatePipe。

constructor(private datePipe: DatePipe)

此外,您还必须将DatePipe添加到组件的提供商列表中

@Component({
  selector: 'my-app',
  template: `
    <div>
    </div>
  `,
  providers: [DatePipe]
})

您将必须在应用程序模块中导入angular公共模块。

import { DatePipe, CommonModule } from '@angular/common'
...
@NgModule({
  imports: [..., CommonModule, .... ],
  ...