I have an object array and need to access the value of each date. I need to go through each date value and check them against the current date.
It’s I’mportant that the returned date value is in the format of DD/MM/YYYY
without showing the time.
P.S apologies, I’m new to Javascript and I’m not quite If I’m even doing this the right way
I appreciate any advise!
const menu = [
{
id: 1,
title: "title of item one",
date: "22/12/2023",
},
{
id: 2,
title: "title of item two",
date: "01/02/2024",
},
{
id: 3,
title: "title of item three",
date: "18/04/2024",
},
]
let objectDate = menu.date;
let date = new Date();
let day = date.getDate();
let month = date.getMonth() + 1;
let year = date.getFullYear();
const currentDate = day + "-" + month + "-" + year;
for (let i=0; i < menu.length; i++) {
if (currentDate > objectDate) {
// objectDate = "Sorry, this offer is expired";
console.log("Sorry this deal is expired");
}
else if (currentDate === objectDate) {
//objectDate = "This offer expires TODAY!";
console.log("Hurry! Expires TODAY");
}
else {
//objectDate = "Offer expires on " + objectDate;
console.log("Expires on " + objectDate);
}
};
3
Answers
right code:
Try to create the object date in for loop. so that you can access the current iteration date menu[i].date. Also create the current date variable with DD/MM/YYYY format instead of DD-MM-YYYY.
Try this two changes.
There are way too many problems in your code, I don’t even know where to start !
Here is a possible solution, you should study it.
PS: Stackoverflow is not a place to receive programming lessons.