Typescript如何过滤数据创建日期和到期日期

发布于 2025-01-13 03:24:41 字数 644 浏览 0 评论 0原文

如何筛选当前日期在创建日期内且当前日期不超过有效期的产品?

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

我不知道逻辑是否 <product.CreatedDate >当前日期 &&产品.ProductEndAt <当前日期>这是正确的,请纠正我,我只想过滤当前日期(Date.now)在 createdDate 内且不超过到期日期的产品

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无可置疑 2025-01-20 03:24:41

您的逻辑应该使用

  • date > CreatedDate - 日期创建日期
  • date ProductEndAt - 日期早于到期日期
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)
}

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)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文