Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

71
Views
How to compare two dates using JavaScript

I want to compare 2 dates. one is "current and" and second is "reset date".

    const todayDate = () => {
      const date = new Date();
      date.setDate(date.getDate()); 
  
      const dd = date.getDate();
      const mm = date.getMonth();
      const y = date.getFullYear();
  
      const currentDate = dd + '/'+ mm + '/'+ y;
      return currentDate
    }

    if (resetDate > todayDate) {
      return res.json(`Sorry, you can't change the username right now. Try again at ${resetDate}`)
    }

I get the reset date from the database and their "structure" looks like that: "1/1/0000"

So I want to check if the reset date is > than today date but it looks like it doesn't work. Does anyone have an idea how can I check if the reset date is > that today date? Like, compare 2 dates.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

First Make sure Your date format is correct. if it's not working then go for the following steps:

  1. You can use moment for this comparison.
  2. Get time from date and match them.

According to me go for moment(https://momentjs.com/). it's tested and easy to use.

about 4 years ago · Juan Pablo Isaza Report

0

You can achieve that with this code:

function dateIsValid( resetDate ) {
  var resetDate = new Date( resetDate );
  if ( resetDate > new Date() ) { // new Date() returns today date
    return false;
  } else {
    return true;
  }
}

function checkDate( resetDate ) {
  if ( !dateIsValid( resetDate ) ) {
    return JSON.stringify(`Sorry, you can't change the username right now. Try again at ${resetDate}`);
  } else {
    // do whatever
    return JSON.stringify('Your username changed successfully');
  }
}

// here is an example:
console.log( checkDate('2020/12/1') );

about 4 years ago · Juan Pablo Isaza Report

0

It seems you are using EU time format dd.mm.yyyy so the first thing would be to slice the date strings to be able to create a Date objects.

And then, once having the Date objects, the getTime() method can be used to retrieve the number of milliseconds since the Unix Epoch (1 jan, 1970) to compare both dates.

const dateOne = createDateObj('23/11/2021'),
      dateTwo = createDateObj('22/03/2022');

function createDateObj(date){
  let d = date.slice(0,2),
      m = date.slice(3,5),
      y = date.slice(6,10);

  return new Date(y,m,d)
}

if(dateOne > dateTwo){
  console.log('date one is more recent than date two')
}
else{
  console.log('date two is more recent than date one')
}

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!