Here is an object array products, each element has a string property named addedDate. Now I want to filter to get only those products added within last 3 day.
let now = new Date();
let newProducts: IProduct[];
newProducts = this.products.filter(p => {
new Date(p.dateAdded).getDate() 3 >= now.getDate()});
console.log(newProducts);
unfortunately, I got nothing filtered out of products. Please help? I
CodePudding user response:
Now I can get what I wanted as below, the solution may need to be optimized though.
let newProducts: IProduct[] = [];
for (let p of this.products)
{
let dTime = new Date(p.dateAdded).getTime();
let nowTime = new Date().getTime();
if (nowTime - dTime < 3 * 86400000)
newProducts.push(p);
}
console.log(newProducts);