skip to Main Content

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


    1. you’re have in JSON a date separated by "/", but set it to a variable via "-"
    2. you are comparing strings when you should compare numbers. Study, if you don’t know, type conversions, and then compare: first year with year, then month with month, and, if the rest are equal, day with day.

    right code:

    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[0].date;
    
    let currentDate = new Date();
    let currentDay = date.getDate();
    let currentMonth = date.getMonth() + 1;
    let currentYear =  date.getFullYear();
    
    
    for (let i=0; i < menu.length; i++) {
    
     if (currentYear >  parseInt(objectDate.slice(0,2)) or currentMonth > parseInt(objectDate.slice(4,6)) or currentDay > parseInt(objectDate.slice(8,10))) {
      // 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);
     }
    };  
    
    Login or Signup to reply.
  1. 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.

    Login or Signup to reply.
  2. 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.

    const
      menu = 
        [ { id: 1, title: 'title of item one',   date: '22/12/2023' } 
        , { id: 2, title: 'title of item two',   date: '30/01/2024' } 
        , { id: 3, title: 'title of item three', date: '18/04/2025' } 
        ] 
    , now  = new Date()  // = today Date + Hours, minutes, seconds, milliseconds
    , DD   = String( now.getDate()    ).padStart(2, '0') // Numeric values must be replaced by a string 
    , MM   = String( now.getMonth() +1).padStart(2, '0') // with a leading zero for 2 full digits.
    , YYYY = now.getFullYear()
    , currentDate = YYYY + MM + DD // in algebraic order, years come first
      ;
    for (let row of menu) 
      {
      let 
        [dd,mm,yyyy] = row.date.split('/') // values are separated by slashes, not hyphens
      , ymd          = yyyy + mm + dd      // transpose d/m/y to ymd
        ;
      switch ( currentDate.localeCompare(ymd) ) // for String type use localeCompare() method
        {
        case 0:
          console.log("Hurry! Expires TODAY -->", row.date );
          break;
        case 1:
          console.log("Sorry this deal is expired -->", row.date );
          break;
        default: // or -1
          console.log("Expires on " + row.date );
        }
      }
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search