Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

174
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda