skip to Main Content

I have this date format returned from api which is local date

"date":"2023-07-14"

i want to compare this date with the current date

i was trying to compare like this

 if(new Date(this.date)==new Date())

but could not find the how to format both the date because both have different formats.

6

Answers


  1. You could just compare only the year, month, and day.

    let d = new Date("2023-07-15"), now = new Date();
    let matches = d.getYear() === now.getYear() 
                && d.getMonth() === now.getMonth()
                && d.getDate() == now.getDate();
    console.log(matches);
    Login or Signup to reply.
  2. To compare the date returned from the API with the current date both dates must have the same format.

    here is an example of code that does this:

    // Get the current date
    let currentDate = new Date().toISOString().split('T')[0];
    
    // Convert the API date 
    let apiDate = new Date("2023-07-14").toISOString().split('T')[0];
    
    // Compare the two dates
    if (apiDate === currentDate) {
      console.log("The API date is equal to the current date.");
    } else if (apiDate < currentDate) {
      console.log("The API date is before the current date.");
    } else {
      console.log("The API date is after the current date.");
    }

    I hope my answer is clear to you

    Login or Signup to reply.
  3. Comparing date strings is enough, but you should add your timezone offset:

    const d = new Date("2023-07-14"); // will be parsed to a UTC date
    d.setMinutes(d.getMinutes() + d.getTimezoneOffset());
    console.log(d.toString())
    
    console.log('is today:', d.toDateString() === new Date().toDateString());
    Login or Signup to reply.
  4. You can get the current date as a string in YYYY-MM-DD format like this:

    const currentDate = (new Date()).toISOString().split('T')[0].toString();
    console.log(currentDate);

    Then simply compare with the API date.

    Login or Signup to reply.
  5. first you should but object in a variable then you should use(>, <, =, and >=) and don’t use(==, !=, ===, or !==)

    check this link for more info : https://sentry.io/answers/how-to-compare-two-dates-with-javascript/

    let date1 = new Date("2023-07-14")
    
    let date2 = new Date("2023-07-14")
    
    
    if(date1 = date2){
      console.log('yes')
    }
    Login or Signup to reply.
  6. You should first convert the date from the API into a Date object and then compare the parts of the date. Like as follows:

    let apiDate = "2023-07-14";
    let current = new Date();
    
    // Convert API date to Date object
    let apiDateObj = new Date(apiDate + "T00:00:00");
    
    function checkDates(current, apiDateObj) {
      let tzOffset = apiDateObj.getTimezoneOffset() * 60000;
      let adjustedDateObj = new Date(apiDateObj.getTime() - tzOffset);
    
      if (adjustedDateObj.toDateString() === current.toDateString()) {
        return "Same date";
      } else {
        return "Different dates";
      }
    }
    
    console.log(checkDates(current, apiDateObj), "checkDates");
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search