-1

How can I bring all the Events that begin inside this day to 30 days? I'd done this pipe but it doesn't work there is an array of events and needs to bring only the events that have dateStart inside 30 days from now, how can I do it work? the compare of dateStart and Date.now() + 30 days

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)
  }
}
  • Please share input/output format & related code – Ritwick Dey May 30 '18 at 17:05
  • you mean, the HTML and component? the thing is in the pipe, I need only bring elements that it inside 30 days from now, inside an array that it's imported and declared in the pipe, if I delete ´>= Date.now() + (30)´ it works, so I only ask how can I do it work, that filter – Christian Vazquez May 30 '18 at 17:11

1 Answers1

1

Oops, not 3 ! JavaScript date object is based on timestamp.

30 Days = 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)
  }
}
Ritwick Dey
  • 18,464
  • 3
  • 24
  • 37