Check if two dates are the same day in JavaScript
Intermediate · 3 min read
Chat apps group messages under "Today". Calendars highlight the current day. A booking form warns you when the start and end are on the same date. All of these need one check: do two dates fall on the same calendar day, whatever the time?
In this tutorial you will write isSameDay(a, b). You will see why comparing timestamps can't answer this question, and why checking only the day number is a bug that hides for a whole month at a time. You need to know how to make a date with new Date(year, month, day, hour, minute) and that months count from 0.
A day is a calendar question
A date in JavaScript is one exact moment, down to the millisecond. "The same day" isn't about how close two moments are. It is about whether they share a calendar page.
Two moments a few seconds apart can be on different days, if midnight falls between them. Two moments 14 hours apart can be on the same day. So measuring the gap can't tell you the answer. You have to look at the calendar parts: the year, the month and the day of the month.
The three getters
Each date has getters that read its local calendar parts:
const d = new Date(2026, 0, 15, 22, 30);
console.log(d.getFullYear(), d.getMonth(), d.getDate()); // 2026 0 15
getMonth() gives 0 for January. getDate() is the day of the month. Don't mix it up with getDay(), which is the day of the week.
Here is the exercise. Try it yourself first if you like, then read on and we will build it step by step.
32 Are two dates on the same day?
fairDecide whether two dates fall on the same calendar day, ignoring the time. Show why comparing timestamps cannot answer this.
Stuck? Three hints, smallest first
- 1. Compare year, month and day. Three comparisons, not one.
- 2. All three are needed: the same day number in a different month is not the same day.
- 3. Two dates 90 minutes apart can be on different days, and two dates 23 hours apart can be on the same one.
Step 1: the wrong turn, timestamps
Start with a morning and an evening on the same day:
const morning = new Date(2026, 0, 15, 8, 0);
const evening = new Date(2026, 0, 15, 22, 30);
console.log(morning.getTime() === evening.getTime()); // false
The timestamps differ by 14 and a half hours, so this check says false. But both moments are on 15 January. Comparing timestamps asks "is this the same moment?", which is a different question.
Step 2: the second wrong turn, only the day number
Since the day of the month is what we care about, compare just that:
const sameDayNumber = (a, b) => a.getDate() === b.getDate();
console.log(sameDayNumber(morning, evening)); // true
console.log(sameDayNumber(new Date(2026, 0, 15), new Date(2026, 1, 15))); // true, but wrong
15 January and 15 February both have day number 15, so this says they are the same day. The bug hides well: it only shows up when the two dates are in different months. That could be weeks after the code ships.
Step 3: compare all three parts
The same day means the same year, the same month and the same day of the month. Join the three checks with && ("and"):
const isSameDay = (a, b) =>
a.getFullYear() === b.getFullYear() &&
a.getMonth() === b.getMonth() &&
a.getDate() === b.getDate();
console.log(isSameDay(morning, evening)); // true
console.log(isSameDay(new Date(2026, 0, 15), new Date(2026, 1, 15))); // false
console.log(isSameDay(new Date(2026, 0, 15), new Date(2027, 0, 15))); // false
Now a different month or a different year is caught.
Step 4: test around midnight
The edge of the day is where bugs live. Try the last millisecond of the 15th and a moment just after midnight:
const justBeforeMidnight = new Date(2026, 0, 15, 23, 59, 59, 999);
const justAfterMidnight = new Date(2026, 0, 16, 0, 0, 30);
console.log(isSameDay(morning, justBeforeMidnight)); // true
console.log(isSameDay(justBeforeMidnight, justAfterMidnight)); // false
console.log(justAfterMidnight - justBeforeMidnight); // 30001
The last two moments are 30,001 milliseconds apart, about 30 seconds, yet they are on different days. 23:59:59.999 still belongs to the 15th, so it matches the morning.
Step 5: a shorter way to write it
toDateString() gives the local date as text, without the time, like 'Thu Jan 15 2026'. Two dates on the same local day give the same text:
console.log(morning.toDateString()); // 'Thu Jan 15 2026'
console.log(morning.toDateString() === evening.toDateString()); // true
This is the same test written differently. The three-getter version makes each part of the check visible, which is why the answer uses it.
The full solution
The panel has isSameDay, then checks a morning and an evening, the very ends of the day, two moments either side of midnight, a different month and a different year. Press Run to see the output.
const isSameDay = (a, b) =>
a.getFullYear() === b.getFullYear() &&
a.getMonth() === b.getMonth() &&
a.getDate() === b.getDate();
const morning = new Date(2026, 0, 15, 8, 0);
const evening = new Date(2026, 0, 15, 22, 30);
const justBeforeMidnight = new Date(2026, 0, 15, 23, 59, 59, 999);
const justAfterMidnight = new Date(2026, 0, 16, 0, 0, 30);
console.log('08:00 and 22:30 on the 15th:', isSameDay(morning, evening));
console.log('23:59:59.999 and 00:00:30 :', isSameDay(justBeforeMidnight, justAfterMidnight), '<- 31ms apart');
console.log('their timestamps differ by', justAfterMidnight - justBeforeMidnight, 'ms');
Output
8 checks that prove this answer
You do not have to read these. They are part of the answer panel above, so pressing
Run there executes all 8 of them and prints ok for each.
That output is the proof, and the build fails if a single check fails.
test('fourteen hours apart, same day', isSameDay(morning, evening), true);
test('the very ends of the day still count', isSameDay(morning, justBeforeMidnight), true);
test('31 milliseconds apart, different days', isSameDay(justBeforeMidnight, justAfterMidnight), false);
test('a date is always the same day as itself', isSameDay(morning, morning), true);
test('the same day number in another month is not the same day', isSameDay(new Date(2026, 0, 15), new Date(2026, 1, 15)), false);
test('nor in another year', isSameDay(new Date(2026, 0, 15), new Date(2027, 0, 15)), false);
test('comparing timestamps answers a different question', morning.getTime() === evening.getTime(), false);
test('comparing the ISO date text is the same test, spelled differently', isSameDay(morning, evening), morning.toDateString() === evening.toDateString());
"Same day" is a question about calendar fields, not about how far apart two instants are. The two assertions in the middle make the point from both sides: 31 milliseconds can straddle a day boundary, and fourteen hours can sit comfortably inside one.
All three fields have to be compared. Checking only getDate() is a bug that hides for a month at a time, which is the worst duration for a bug to hide.
Common mistakes
- Comparing timestamps. That asks "same moment?", not "same day?".
- Comparing only
getDate(). 15 January and 15 February look the same. Check the month and year too. - Using
getDay()instead ofgetDate().getDay()is the weekday, so every Thursday would match.
Try it yourself
- Write
isToday(date, now)usingisSameDay. Passnowin so you can test it. - Write
isSameMonth(a, b)that ignores the day. - Group an array of message times into days, using
toDateString()as the key.