Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Typescript how to filter data created date and expiration date

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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)
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading