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

170
Views
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 answers
Answer question

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 Report

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 Report

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 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!