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

168
Vistas
How to compare two dates without hours in javascript?

I have two dates date and meeting.date coming from two APIs. I want to check, if the dates are equal.

// date = "28.02.2022";
// meeting.date = "2022-02-08 14:30:00";

const firstDate = new Date(`${date.split(".").reverse().join("-")}`);
const secondDate = new Date(`${meeting.date.split(" ")[0]}`)

firstDate.setHours(0,0,0,0);
secondDate.setHours(0,0,0,0);

console.log(firstDate, secondDate, firstDate === secondDate);

This logs me

[Log] Mon Feb 28 2022 00:00:00 GMT+0100 (CET)  – Mon Feb 28 2022 00:00:00 GMT+0100 (CET)  – false

I expect that to be true? I found the solution, setting the hours to zero here on stackoverflow, but I'd say, that this is gratuitous, as I don't pass the time the Date. Anyhow, what am I doing wrong?

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You are comparing the Date objects themselves - and they will never be equal unless it is the same "object" pointed to by 2 different variables, e.g.

const dateOne = dateTwo = new Date();

You must either compare the date parts as Strings, e.g.

console.log(
  firstDate,
  secondDate,
  firstDate.toISOString().substr(0, 10) === secondDate.toISOString().substr(0, 10)
);

or compare the dates as Numbers (in milliseconds) which is the recommended way:

console.log(
  firstDate,
  secondDate,
  firstDate.getTime() === secondDate.getTime()
);
about 4 years ago · Juan Pablo Isaza Denunciar

0

What you are doing here is comparing two different instances (two different references) of Date object, which lead to an unexpected result

You could try to compare the millisecond of these by using getTime

const date = "28.02.2022";
const meeting = { date: "2022-02-28 14:30:00" }

const firstDate = new Date(`${date.split(".").reverse().join("-")}`);
const secondDate = new Date(`${meeting.date.split(" ")[0]}`)

firstDate.setHours(0,0,0,0);
secondDate.setHours(0,0,0,0);

console.log(firstDate, secondDate, firstDate.getTime() === secondDate.getTime());

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use .getTime() method to convert both of the dates to the number of milliseconds since the ECMAScript epoch and then compare them.

firstDate.setHours(0,0,0,0).getTime();
secondDate.setHours(0,0,0,0).getTime();
console.log(firstDate, secondDate, firstDate === secondDate);

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