3

I have two javascript dates and I want to know if they're the same date. The two dates can be for the same date but have different times: Date1 can be set for 3PM while Date2 can be set for 1AM.

I tried these two options but neither work:

// doesn't work when same date but different time
if (Date1.getTime() === Date2.getTime()) 

// gives true when dates are in different months
if (Date1.getUTCDate() === Date2.getUTCDate())  

What the best way to get true when Date1 and Date2 are the same day, regardless of the actual time within the day?

Thanks.

4 Answers 4

1

You can get the day, month and year of the dates and check if they are equal.

if (date1.getUTCDate() == date2.getUTCDate() && 
    date1.getUTCMonth() == date2.getUTCMonth() && 
    date1.getUTCFullYear() == date2.getUTCFullYear()) {
 // dates are on the same day
}
Sign up to request clarification or add additional context in comments.

2 Comments

Shouldn't it be getDate() ?? I edit your answer. It seems to be the best way to do it.
@frenchie: Yes, you are correct. getDay() gets the day of the week which wouldn't work. Thanks for fixing it! :)
1

Why don't you use toDateString for your comparison?

2 Comments

Is there a catch with UTC comparison?
I've been burned with UTC and Local before.
1
var equalDates = function (a, b) {
  var a_time = a.getTime();
  var b_time = b.getTime();

  var a_days = Math.floor(a_time / (24 * 3600 * 1000));
  var b_days = Math.floor(b_time / (24 * 3600 * 1000));

  return (a_days === b_days);
};

You first get the times in miliseconds. Then you round down to days (since 1970-01-01) and compare those.

Comments

1

Use the setHours: function on your date object and set hours, minutes, seconds and milliseconds to zero:

var today = new Date().setHours(0,0,0,0)

For your specific example, you could use:

if(date1.setHours(0,0,0,0) === date2.setHours(0,0,0,0))

where date1 and date2 are your date objects.

setHours:

Sets the hours for a specified date according to local time, and returns the number of milliseconds since 1 January 1970 00:00:00 UTC until the time represented by the updated Date instance.

EXAMPLE:

var today1 = new Date().setHours(0,0,0,0); 
var today2 = new Date().setHours(1,0,0,0); //notice the 1 here

var today3 = new Date().setHours(0,0,0,0); 
var today4 = new Date().setHours(0,0,0,0);

console.log(today1 === today2); //returns false
console.log(today3 === today4); //returns true

4 Comments

@frenchie - The concept is much cleaner than checking each element of the date independently.
True but look at the number of lines and the gymnastics involved. In truth, it's a matter of personal preference but I prefer less code. I know it works, which is why I upvoted.
@frenchie - I completely understand. Although the number of lines is simply a "for instance" and can be reduced to merely if(date1.setHours(0,0,0,0) === date2.setHours(0,0,0,0)) where date1 and date2 are your date objects. Either way, I can't argue with personal preference :D
Yup, programming is part science, part art. Thanks for your answer; I might use the concept of date1.setHours(0,0,0,0) in other parts of my future code. Happy coding!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.