Emmanuel Akinfulubi

JavaScriptDate

Why new Date('YYYY-MM-DD') is UTC in JavaScript

Intermediate · 4 min read

You save a birthday as 2026-01-15, turn it back into a date with new Date('2026-01-15'), and the page shows 14 January. Your code looks fine. This is one of the most reported date bugs in JavaScript, and it comes from one rule about how date text is read. In this tutorial you will see what new Date('2026-01-15') really means, why the day can shift, and two ways to get the day you meant.

You need to know how to make a date and use getDate(), which gives the day of the month.

Local time and UTC

Every date is one exact moment. You can read it in local time, the clock of your own time zone, or in UTC, the world's reference clock at Greenwich. Midnight UTC is not midnight everywhere. At midnight UTC on 15 January:

  • in Lagos (one hour ahead) it is 1:00 AM on the 15th;
  • in New York (five hours behind in winter) it is 7:00 PM on the 14th.

So the same moment can sit on two different calendar days.

The rule for date text

When you give new Date text in ISO form, the standard says:

  • Date only, such as '2026-01-15': read as midnight UTC.
  • Date and time with no zone, such as '2026-01-15T00:00:00': read as local time.

One added time, and the meaning changes. That is the whole bug.

Here is the exercise. Have a go yourself first if you like, then read on and we will build it together.

17 The trap: a bare YYYY-MM-DD string is UTC midnight

fair

new Date('2026-01-15') does not mean midnight where you are. Show what it does mean, and show what changes when you add a time.

Stuck? Three hints, smallest first
  1. 1. A date-only ISO string is defined to be UTC. A date-time string with no offset is defined to be local.
  2. 2. So compare the timestamp against Date.UTC(2026, 0, 15) rather than against a local date.
  3. 3. West of Greenwich, the local day of that date is the 14th. That is the bug this exercise is about.

Step 1: parse the bare date

Step 1

const parsed = new Date('2026-01-15');
console.log(parsed.toISOString()); // '2026-01-15T00:00:00.000Z'
console.log(parsed.getTime() === Date.UTC(2026, 0, 15)); // true

toISOString() shows UTC, and it says midnight on the 15th. Date.UTC(2026, 0, 15) builds the timestamp for midnight UTC on 15 January (months count from 0), and the two numbers match. So the text was read as UTC.

Step 2: read it back in local time

Step 2

console.log(parsed.getUTCDate()); // 15
console.log(parsed.getDate());    // 15 in Lagos; 14 in New York

getUTCDate() reads the day in UTC, so it is always 15. getDate() reads it in local time. Anywhere west of Greenwich, midnight UTC is still the evening before, so you get 14. That is the "one day out" bug: the date was stored right, but read in a different zone from the one it was made in.

If your computer is in Lagos or anywhere east, you won't see the bug yourself. Your users in the Americas will.

Step 3: add a time, and it becomes local

Step 3

const withTime = new Date('2026-01-15T00:00:00');
console.log(withTime.getDate(), withTime.getHours()); // 15 0

Now the text has a time and no zone, so it is read as local midnight. getDate() is 15 in every zone, because the date was made in the reader's own zone in the first place.

Step 4: the clearest fix, build from parts

When '2026-01-15' means a calendar day, such as a birthday or a due date, split it and build a local date from the numbers:

Step 4

function localDate(text) {
  const [y, m, d] = text.split('-').map(Number);
  return new Date(y, m - 1, d);
}
console.log(localDate('2026-01-15').getDate()); // 15
console.log(localDate('2026-01-15').toDateString()); // 'Thu Jan 15 2026'

split('-') gives ['2026', '01', '15'], and map(Number) turns each piece into a number. [y, m, d] puts the three numbers in three names. Subtract 1 from the month because new Date counts months from 0. The result is local midnight on the 15th, wherever the code runs.

When UTC is what you want

Sometimes you want an exact moment, not a calendar day: when a payment happened, for example. Then keep the full ISO text with its zone, like '2026-01-15T09:30:00Z', and read it with the UTC getters. The Z removes any doubt.

The answer on the page follows the same idea. Its check on getDate() accepts 14 or 15, because both are correct depending on where the code runs. All its other checks use UTC getters or timestamps, which are the same everywhere.

The full solution

The panel parses both strings and prints the UTC and local day of each. What the local line says depends on your time zone. Press Run to see your own result.

The answer

const parsed = new Date('2026-01-15');          // date only  -> UTC midnight
const withTime = new Date('2026-01-15T00:00:00'); // has a time -> LOCAL midnight

console.log('bare string   :', parsed.toISOString());
console.log('  UTC date    :', parsed.getUTCDate(), '<- always 15');
console.log('  local date  :', parsed.getDate(), '<- 15 here, 14 west of Greenwich');
console.log('with a time   :', withTime.getDate(), '<- always 15, in any zone');

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('the bare string is UTC midnight', parsed.getTime(), Date.UTC(2026, 0, 15));
test('which is what its ISO form says', parsed.toISOString(), '2026-01-15T00:00:00.000Z');
test('the UTC day is the 15th', parsed.getUTCDate(), 15);
test('and the UTC hour is 0', parsed.getUTCHours(), 0);
test('but the LOCAL day is the 14th or the 15th, depending on the zone', [14, 15].includes(parsed.getDate()), true);
test('adding a time makes it local, so the day is always the 15th', withTime.getDate(), 15);
test('and its local hour is midnight', withTime.getHours(), 0);
test('Date.parse agrees with the constructor', Date.parse('2026-01-15'), Date.UTC(2026, 0, 15));

This is the bug behind most "the date is one day out" reports. '2026-01-15' is parsed as UTC midnight; '2026-01-15T00:00:00' is parsed as local midnight. One character of difference, up to a day of difference in the answer.

The fifth assertion is written as a set membership rather than a fixed value on purpose. The local day genuinely differs by zone — 15 in Kiritimati, 14 in Niue — so pinning it to one number would only be asserting where this build happened to run.

The habit that avoids the whole problem: to get a local calendar date out of a YYYY-MM-DD string, split it and use new Date(y, m - 1, d). To keep an instant, keep the full ISO string with its Z.

Common mistakes

  • Parsing a bare YYYY-MM-DD and reading it with getDate(). It is midnight UTC, so west of Greenwich the local day is the day before.
  • Testing only on your own machine. East of Greenwich the bug is invisible. Try your code with a zone such as New York.
  • Fixing it by adding a day. That breaks it for everyone east of Greenwich. Build the date from parts instead.

Try it yourself

  1. Run your code with TZ=America/New_York node file.js and watch parsed.getDate() change.
  2. Write toLocalIso(date) that turns a local date back into YYYY-MM-DD text using the local getters.
  3. What does new Date('2026-01-15T00:00:00Z').getUTCHours() give? What about getHours() in Lagos?

Keep practising

Newsletter

New writing, when it lands.

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