Calculate the days until a birthday in JavaScript
Intermediate · 3 min read
"Only 114 days until Christmas!" Countdowns like this are everywhere: birthdays, exam dates, sales, the end of a free trial. They all come down to one question: how many whole days are there between two dates? In this tutorial you will answer it in a way that stays right all year.
You will write a function called daysBetween(from, to). It takes two dates and returns a whole number of days: positive if to is later, negative if it is earlier, and 0 for the same day. You need to know what a function is and how to create a date with new Date(year, month, day). Remember that months count from 0, so September is 8.
Dates are numbers underneath
Inside JavaScript, a date is stored as a number: the milliseconds since 1 January 1970. When you subtract one date from another, you get the gap in milliseconds.
const a = new Date(2026, 8, 2);
const b = new Date(2026, 8, 3);
console.log(b - a); // 86400000
One day is 24 hours × 60 minutes × 60 seconds × 1000 milliseconds, which is 86,400,000. Divide by that and you have days.
Moments and days are different
A Date is a moment, with hours and minutes, not just a day. 9 AM today and 11 PM today are 14 hours apart, but they are the same day. If you don't remove the time first, those hours turn into a fraction of a day and spoil the answer.
There is a second, sneakier problem. In places that use daylight saving time, one day each spring has only 23 hours. Divide 23 hours by 24 and you get 0.958, not 1.
Here is the exercise. Try it yourself first if you like, or build it with the steps below.
6 How many days until a given date
fairWork out the number of days between today and a target date, such as a birthday later in the year.
Stuck? Three hints, smallest first
- 1. Subtracting two dates gives milliseconds. Divide by 86,400,000 to get days.
- 2. Zero the time on both dates first, or a few hours’ difference will round the answer wrongly.
- 3. Use
Math.roundrather thanMath.floor, so daylight-saving shifts of an hour do not lose a day.
Step 1: subtract and divide
Name the number of milliseconds in a day, so the code says what it means.
const MS_PER_DAY = 24 * 60 * 60 * 1000;
const today = new Date(2026, 8, 2); // 2 September 2026
const christmas = new Date(2026, 11, 25); // 25 December 2026
console.log((christmas - today) / MS_PER_DAY); // 114
This works because both dates start at midnight.
Step 2: see the time get in the way
Now try two moments on the same day:
const MS_PER_DAY = 24 * 60 * 60 * 1000;
const morning = new Date(2026, 8, 2, 9, 0);
const evening = new Date(2026, 8, 2, 23, 0);
console.log((evening - morning) / MS_PER_DAY); // 0.5833333333333334
The answer should be 0. We need to compare days, not moments.
Step 3: strip the time
Build a new date from only the year, month and day. Leaving out the hours sets the time to midnight.
function atMidnight(date) {
return new Date(date.getFullYear(), date.getMonth(), date.getDate());
}
console.log(atMidnight(new Date(2026, 8, 2, 23, 0)).getHours()); // 0
getDate() is the day of the month. This function returns a new date, so the date you passed in is not changed.
Step 4: round, don't floor
After stripping the time, the result should already be a whole number. On a day when the clocks change, it can be a little off, like 0.958. Math.floor always rounds down, so 0.958 would become 0, a day lost. Math.round goes to the nearest whole number, so 0.958 becomes 1.
console.log(Math.floor(0.958)); // 0
console.log(Math.round(0.958)); // 1
Step 5: put it together
const MS_PER_DAY = 24 * 60 * 60 * 1000;
function atMidnight(date) {
return new Date(date.getFullYear(), date.getMonth(), date.getDate());
}
function daysBetween(from, to) {
const a = atMidnight(from);
const b = atMidnight(to);
return Math.round((b - a) / MS_PER_DAY);
}
const today = new Date(2026, 8, 2);
const christmas = new Date(2026, 11, 25);
console.log(daysBetween(today, christmas)); // 114
console.log(daysBetween(christmas, today)); // -114
console.log(daysBetween(new Date(2026, 8, 2, 9, 0), new Date(2026, 8, 2, 23, 0))); // 0
A negative answer means the target is in the past. That is useful: a countdown can show "114 days to go" or "114 days ago" from the same number.
Two more cases worth testing are a leap year and the turn of a year:
console.log(daysBetween(new Date(2024, 1, 28), new Date(2024, 2, 1))); // 2
console.log(daysBetween(new Date(2026, 11, 31), new Date(2027, 0, 1))); // 1
From 28 February to 1 March 2024 is 2 days because 2024 had a 29 February.
The full solution
The complete answer prints the days to Christmas, the days since New Year, and the same-day case that shows why the time is stripped. Press Run to see it.
const MS_PER_DAY = 24 * 60 * 60 * 1000;
// Strip the time so we compare whole days, not moments.
function atMidnight(date) {
return new Date(date.getFullYear(), date.getMonth(), date.getDate());
}
function daysBetween(from, to) {
const a = atMidnight(from);
const b = atMidnight(to);
// Math.round, not floor: a daylight-saving change makes some days 23 hours.
return Math.round((b - a) / MS_PER_DAY);
}
const today = new Date(2026, 8, 2); // 2 September 2026
const christmas = new Date(2026, 11, 25); // 25 December 2026
console.log('days to Christmas:', daysBetween(today, christmas));
console.log('days since new year:', daysBetween(new Date(2026, 0, 1), today));
// Why the time must be stripped: without it, these two would differ.
const morning = new Date(2026, 8, 2, 9, 0);
const evening = new Date(2026, 8, 2, 23, 0);
console.log('same day, different times ->', daysBetween(morning, evening));
Output
6 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 6 of them and prints ok for each.
That output is the proof, and the build fails if a single check fails.
test('to Christmas', daysBetween(today, christmas), 114);
test('same day is zero', daysBetween(morning, evening), 0);
test('tomorrow is one', daysBetween(today, new Date(2026, 8, 3)), 1);
test('backwards is negative', daysBetween(christmas, today), -114);
test('across a leap day', daysBetween(new Date(2024, 1, 28), new Date(2024, 2, 1)), 2);
test('across a year boundary', daysBetween(new Date(2026, 11, 31), new Date(2027, 0, 1)), 1);
Two details make this correct rather than nearly correct. Zeroing the time means 9am today to 11pm today is 0 days, not a fraction. And Math.round instead of Math.floor survives daylight saving, where one day of the year is 23 hours long and would otherwise come out as 0.958 and floor to zero. The leap-day assertion confirms February 2024 had a 29th.
Common mistakes
- Not stripping the time. Two moments on the same day give a fraction, and 11 PM one night to 1 AM the next morning rounds to 0 days, although the dates are one day apart.
- Using
Math.floor. On a 23-hour daylight-saving day, floor drops a whole day. UseMath.round. - Forgetting months count from 0.
new Date(2026, 12, 25)is not December; it rolls over into January 2027.
Try it yourself
- Write
daysUntilBirthday(today, month, day)that uses this year's birthday, or next year's if it has already passed. - Make the countdown say "tomorrow" for 1, "today" for 0 and "in N days" otherwise.
- Work out how many days old you are using your date of birth and
new Date().