75

The JavaScript Date object compare dates with time including, so, if you compare: time1.getTime() === time2.getTime(), they'll be "false" if at least one millisecond is different.

What we need is to have a nice way to compare by Hour, Day, Week, Month, Year? Some of them are easy, like year: time1.getYear() === time2.getYear() but with day, month, hour it is more complex, as it requires multiple validations or divisions.

Is there any nice module or optimized code for doing those comparisons?

Kostanos
  • 9,615
  • 4
  • 51
  • 65
reza
  • 5,972
  • 15
  • 84
  • 126
  • Same day as in exact date or same day of the month (independent of year) or...? – Rob M. May 08 '17 at 18:55
  • 1
    Also, take a look at the moment manual, [there are functions built specifically for this task](http://momentjs.com/docs/#/query/is-same/) – Rob M. May 08 '17 at 18:57
  • 2
    `d1.getDate() === d2.getDate() && Math.abs(d1.getTime() - d2.getTime())<24*60*60*1000` – BlueMice Nov 06 '17 at 06:35
  • And of course, to add to this, [you dont need momentjs](https://github.com/you-dont-need/You-Dont-Need-Momentjs) and might want to use smaller libraries – phil294 Aug 12 '19 at 19:51

2 Answers2

117

The Date prototype has APIs that allow you to check the year, month, and day-of-month, which seems simple and effective.

You'll want to decide whether your application needs the dates to be the same from the point of view of the locale where your code runs, or if the comparison should be based on UTC values.

function sameDay(d1, d2) {
  return d1.getFullYear() === d2.getFullYear() &&
    d1.getMonth() === d2.getMonth() &&
    d1.getDate() === d2.getDate();
}

There are corresponding UTC getters getUTCFullYear(), getUTCMonth(), and getUTCDate().

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • 16
    perhaps it might be a bit more efficient to check the day first (`getDate`). If we are checking dates close together, the year and month will likely be the same but the day is more likely differ. We could save some checks by short circuiting out of the function after only one comparison. – Paul Rooney Oct 24 '19 at 05:54
  • 2
    @PaulRooney that is a good point; similarlly an initial range check on the raw timestamp could be worthwhile. In a modern runtime I suspect such improvements would only matter if the comparison were made extremely often. – Pointy Oct 24 '19 at 11:19
  • 3
    @JonasMerhej this will not work with numbers, because you will get false positives. For example 5th of may (day 5, month 4) will be the same as 4th of july (day 4, month 5) – newfolder Apr 14 '20 at 08:16
  • 1
    @newfolder excuse my bad explanation. You're completely right I meant to add is as a string, e.g '4+5' will be different than '5+4' – BonisTech Apr 14 '20 at 20:09
  • 1
    Sad. Python is just superior. – Tobias Feil Aug 30 '20 at 06:32
  • @PaulRooney Thx for the feedback, the code is now updated :) – Raphaël Balet Jul 13 '23 at 08:52
15
var isSameDay = (dateToCheck.getDate() === actualDate.getDate() 
     && dateToCheck.getMonth() === actualDate.getMonth()
     && dateToCheck.getFullYear() === actualDate.getFullYear())

That will ensure the dates are in the same day.

Read more about Javascript Date to string

Daniel Taub
  • 5,133
  • 7
  • 42
  • 72