Check if a date is within 30 days with Angular-cli pipes

387 Views Asked by At

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)
  }
}
1

There are 1 best solutions below

0
Ritwick Dey On

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)
  }
}