使用Angular-cli管道检查日期是否在30天内

时间:2018-05-30 17:02:38

标签: typescript angular-cli angular2-pipe

如何将今天开始的所有活动带到30天? 我做过这个管道,但它不起作用 有一系列事件,需要在30天内只带来dateStart的事件,我该怎么做呢? dateStartDate.now() + 30天

的比较
import { Pipe, PipeTransform } from '@angular/core';
import { SupplyEvent } from "../../models/supplyEvent";

@Pipe({
  name: 'toBeginFilter'
})
export class ToBeginFilterPipe implements PipeTransform {

  toBegin (SupplyEvent) {
    if (SupplyEvent.dateStart >= Date.now() + (30)) {
      return SupplyEvent
    }
  }

  transform(SupplyEvents: SupplyEvent[]): any {
    if (!SupplyEvents) return SupplyEvents
    return SupplyEvents.filter(this.toBegin)
  }
}

1 个答案:

答案 0 :(得分:1)

哎呀,不是3! JavaScript日期对象基于时间戳。

  

30天= 30 * 24 * 3600 * 1000

export class ToBeginFilterPipe implements PipeTransform {

  // assuming SupplyEvent.dateStart is timestamp.
  toBegin (SupplyEvent) {
    return Date.now() - SupplyEvent.dateStart <= (30*24*3600*1000);
  }

  transform(SupplyEvents: SupplyEvent[]): any {
    if (!SupplyEvents) return SupplyEvents
    return SupplyEvents.filter(this.toBegin)
  }
}