Emmanuel Akinfulubi

JavaScriptDate

Add a day vs add 24 hours (DST) in JavaScript

Advanced · 4 min read

"Remind me at this time tomorrow." The natural way to write that in code is to add one day's worth of milliseconds, 86,400,000. Most of the year, that works. But in places that change their clocks for daylight saving time, it gives the wrong hour twice a year, and your 9:00 reminder fires at 8:00 or 10:00.

In this tutorial you will compare two ways of moving a date on by one day: adding 86,400,000 milliseconds, and calling setDate(getDate() + 1). You will see when they differ, why, and how to test the difference without knowing which time zone the code runs in. You need to know what a timestamp is and how setDate works.

Daylight saving time in one paragraph

In many countries the clocks jump forward one hour in spring and back one hour in autumn. This is daylight saving time, or DST. On the spring day, the local day has only 23 hours. On the autumn day, it has 25. Other places, such as Lagos, never change their clocks, so every local day there has 24 hours.

Two different questions

  • "Exactly 24 hours from now" is a duration. Adding milliseconds answers it.
  • "The same time on the next calendar day" is a calendar step. setDate answers it.

On a normal day, both give the same moment. On a clock-change day, they don't.

Here is the exercise. Try it on your own first if you like, then read on and we will build it step by step.

53 Adding a day is not adding 24 hours

hard

Move a date on by one day two ways: by adding 86,400,000 milliseconds, and by setDate(getDate() + 1). Show what separates them, without assuming which zone the code is running in.

Stuck? Three hints, smallest first
  1. 1. Adding milliseconds moves the instant. setDate moves the calendar day and keeps the wall clock.
  2. 2. They differ by exactly the change in the zone offset between the two dates.
  3. 3. getTimezoneOffset() lets you write that as an identity, which is true in every zone including ones with no daylight saving.

Step 1: add the milliseconds

Step 1

const MS_PER_DAY = 86400000;
const noon = new Date(2026, 5, 15, 12, 0, 0); // 15 June 2026, 12:00 local time
const byMilliseconds = new Date(noon.getTime() + MS_PER_DAY);
console.log(byMilliseconds.getTime() - noon.getTime()); // 86400000

getTime() reads the timestamp, the milliseconds since 1970. Adding to it moves the moment forward by exactly that amount. This is always true, in every zone.

Step 2: take a calendar step

Step 2

const byCalendar = new Date(noon);
byCalendar.setDate(byCalendar.getDate() + 1);
console.log(byCalendar.getDate(), byCalendar.getHours()); // 16 12

setDate changes the day of the month and keeps the clock time. The copy, new Date(noon), makes sure noon itself doesn't change. We land on the 16th, still at 12:00.

Step 3: see them disagree

15 June isn't near a clock change, so let's pick a day that is. In the UK, clocks go forward on 29 March 2026. On a computer set to London time:

Step 3

const beforeChange = new Date(2026, 2, 28, 12, 0, 0); // 28 March 2026, 12:00
const plusMs = new Date(beforeChange.getTime() + MS_PER_DAY);
const plusDay = new Date(beforeChange);
plusDay.setDate(plusDay.getDate() + 1);
console.log(plusMs.getHours());  // 13 in London, 12 in Lagos
console.log(plusDay.getHours()); // 12

In London, 29 March has only 23 hours. Adding a full 24 hours goes one hour past noon, to 13:00. The calendar step stays at 12:00. In Lagos, both give 12:00, because there is no clock change.

So which is right? It depends on the question. A daily reminder at noon should stay at noon, so it needs the calendar step. A 24-hour parking ticket really does run out 24 hours later, so it needs the milliseconds.

Step 4: the gap is the change in offset

getTimezoneOffset() tells you how many minutes your local time is behind UTC, the world's reference clock. A negative number means ahead of UTC. In London it is 0 in winter and -60 in summer. In Lagos it is always -60.

JavaScript

console.log(beforeChange.getTimezoneOffset()); // 0 in London, -60 in Lagos
console.log(plusDay.getTimezoneOffset());      // -60 in London and in Lagos

The two results always differ by exactly the change in offset between the two dates, turned into milliseconds:

Step 4

const gap = plusDay.getTime() - plusMs.getTime();
const offsetChange = (plusDay.getTimezoneOffset() - beforeChange.getTimezoneOffset()) * 60000;
console.log(gap === offsetChange); // true
console.log(gap);                  // -3600000 in London, 0 in Lagos

In London the offset changed by 60 minutes, so the gap is one hour, 3,600,000 milliseconds. It is negative because the calendar step lands an hour before the 24-hour jump. In Lagos the offset didn't change, so both sides are 0.

The gap === offsetChange line is true everywhere. That is how the tested answer is checked: instead of saying "they differ by an hour", which is only true in some places on two days a year, it checks this relationship, which holds wherever the code runs.

Step 5: many days on

The calendar step keeps the clock time over any distance:

Step 5

const hundredDays = new Date(noon);
hundredDays.setDate(hundredDays.getDate() + 100);
console.log(hundredDays.getMonth(), hundredDays.getDate(), hundredDays.getHours()); // 8 23 12
console.log(noon.getDate(), noon.getHours()); // 15 12

100 calendar days after 15 June is 23 September (month 8), still at 12:00. And noon is untouched, because every change was made on a copy.

The full solution

The panel starts from noon on 15 June 2026 and moves it both ways. It prints the hours and whether the offset changed on your machine. The checks cover the exact duration, the calendar step, the offset relationship and the 100-day jump. Press Run to see them.

The answer

const MS_PER_DAY = 86400000;

const noon = new Date(2026, 5, 15, 12, 0, 0);    // 15 June 2026, local midday

// Two different operations that people treat as the same one.
const byMilliseconds = new Date(noon.getTime() + MS_PER_DAY);

const byCalendar = new Date(noon);
byCalendar.setDate(byCalendar.getDate() + 1);

// A hundred calendar days on, still at midday whatever happens to the clocks.
const hundredDays = new Date(noon);
hundredDays.setDate(hundredDays.getDate() + 100);

const offsetChanged = byCalendar.getTimezoneOffset() !== noon.getTimezoneOffset();

console.log('start        :', noon.getHours() + ':00 on the', noon.getDate() + 'th');
console.log('+86400000ms  :', byMilliseconds.getHours() + ':00 on the', byMilliseconds.getDate() + 'th');
console.log('+1 calendar day:', byCalendar.getHours() + ':00 on the', byCalendar.getDate() + 'th');
console.log('did the offset change between them?', offsetChanged, '(false in a zone with no DST)');

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.

8 checks

test('adding milliseconds moves the instant by exactly that much', byMilliseconds.getTime() - noon.getTime(), MS_PER_DAY);
test('the calendar step keeps the wall clock at midday', byCalendar.getHours(), 12);
test('and lands on the next date', byCalendar.getDate(), 16);
test('the gap between the two is exactly the change in zone offset', byCalendar.getTime() - byMilliseconds.getTime(), (byCalendar.getTimezoneOffset() - noon.getTimezoneOffset()) * 60000);
test('so where the offset did not change, they are the same instant', offsetChanged ? true : byCalendar.getTime() === byMilliseconds.getTime(), true);
test('a hundred calendar days on is 23 September 2026', [hundredDays.getMonth(), hundredDays.getDate()], [8, 23]);
test('and the clock still reads midday', hundredDays.getHours(), 12);
test('while the input was never modified', [noon.getDate(), noon.getHours()], [15, 12]);

These are two different questions. "This time tomorrow" is a calendar operation and setDate answers it. "86,400,000 milliseconds from now" is a duration and adding to the timestamp answers that. In a zone with daylight saving they give different instants twice a year, and the one that is wrong depends on which question you were actually asking. A daily reminder at 09:00 must not drift to 08:00 in October, so it is a calendar operation.

The fourth assertion is how you test this without knowing where the code runs. Rather than asserting "they differ by an hour" — true only in some zones, on two days of the year — it asserts the relationship: the difference between the two results is exactly the change in getTimezoneOffset() across the boundary. In Lagos or in UTC that change is zero and both sides are zero; in London on the last Sunday of March it is 60 minutes and both sides are 3,600,000. The identity holds either way, which is what makes it worth asserting.

Common mistakes

  • Adding 86,400,000 for "tomorrow at the same time". On a clock-change day the hour moves. Use setDate.
  • Testing only on your own machine. If your zone has no DST, both ways always agree and the bug stays hidden. Try TZ=Europe/London node file.js.
  • Changing the original date. setDate works in place. Copy with new Date(date) first.

Try it yourself

  1. Repeat Step 3 for 25 October 2026, when UK clocks go back. What hour does adding milliseconds give?
  2. Write addCalendarDays(date, n) and addHours(date, n) as two separate functions with clear names.
  3. Print getTimezoneOffset() for the 1st of every month in 2026 and see whether your zone changes.

Keep practising

Newsletter

New writing, when it lands.

Occasional notes on shipping software with AI agents. No schedule, no filler, unsubscribe in one click.