Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

75
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda