Home > Software engineering >  Typescript how to filter data created date and expiration date
Typescript how to filter data created date and expiration date

Time:03-11

how can I filter the product whose current date is within the created date and the current date does not exceed the expiration date?

async getAllproduct():Promise<Array<Product>>{
   const currentDate = new Date(Date.now() - 86400 * 7000);
   const product: Array<Product> = await this.findProductStatus(
      currentDate,
      ProductStatus.active
   );
   const activeProduct = product.filter((product) => product.CreatedDate > currentDate && product.ProductEndAt < currentDate);
   return activeProduct;
}

I don't know if the logic <product.CreatedDate > currentDate && product.ProductEndAt < currentDate> here is right, please correct me on this, I just want to filter the product that has the current date (Date.now) is within the createdDate and does not exceed the expiration date

CodePudding user response:

Your logic should use

  • date > CreatedDate - date is after the created date
  • date < ProductEndAt - date is before the expiration date
async getAllproduct(): Promise<Array<Product>> {
   const lastWeek = new Date()
   lastWeek.setDate(lastWeek.getDate() - 7)

   const products: Array<Product> = await this.findProductStatus(
      lastWeek,
      ProductStatus.active
   );

   return products.filter(({ CreatedDate, ProductEndAt }) =>
     lastWeek > CreatedDate && lastWeek < ProductEndAt)
}
  • Related