Emmanuel Akinfulubi

JavaScriptDate

Format a date as YYYY-MM-DD in JavaScript

Beginner · 4 min read

2026-01-05 is the clearest way to write a date. Big parts come first: year, then month, then day. It is called ISO format, after the international standard that defines it. Databases, APIs, file names and the <input type="date"> field on web forms all use it. In this tutorial you will build that text from a JavaScript date, with two digits for the month and day every time.

There is a famous shortcut for this, and it is wrong for part of every day. You will see why, so you never ship the bug where a task saved late at night shows up on the wrong day. You need to know how to make a date and that getMonth() counts from 0.

Padding a number with zeros

getDate() on 5 January gives the number 5, but ISO wants 05. Numbers don't have leading zeros, so you turn the number into text and pad it. padStart(length, fill) adds fill to the front of a string until it is length characters long:

JavaScript

console.log(String(5).padStart(2, '0'));  // '05'
console.log(String(25).padStart(2, '0')); // '25'

25 is already two characters long, so nothing is added.

Local time and UTC

Every date in JavaScript is one exact moment. You can read that moment in two ways. Local time is the clock on your computer, in your time zone. UTC is the world's reference clock, the time at Greenwich with no daylight saving. Lagos is one hour ahead of UTC, so when it is 23:30 in Lagos, it is 22:30 in UTC. Near midnight, the two clocks can be on different days. Keep that in mind for Step 4.

Here is the exercise. Try it first if you like, then read on and we will build it together.

5 Format a date as YYYY-MM-DD by hand

easy

Produce ISO calendar form, 2026-01-05, from a local date. Two digits for month and day, always.

Stuck? Three hints, smallest first
  1. 1. String(n).padStart(2, '0') is the padding.
  2. 2. Add one to getMonth() before padding it.
  3. 3. toISOString().slice(0, 10) looks like a shortcut, but it converts to UTC first.

Step 1: a padding helper

You will pad twice, so give it a name:

Step 1

const pad2 = (n) => String(n).padStart(2, '0');
console.log(pad2(1), pad2(12)); // 01 12

Step 2: join the three parts

Read the local year, month and day, and glue them together with dashes. Remember to add 1 to the month:

Step 2

function toIsoDate(date) {
  return date.getFullYear() + '-' + pad2(date.getMonth() + 1) + '-' + pad2(date.getDate());
}
const fifth = new Date(2026, 0, 5);
console.log(toIsoDate(fifth)); // '2026-01-05'
console.log(toIsoDate(new Date(2026, 11, 25))); // '2026-12-25'

Without the + 1, January would come out as 00. Without pad2, you would get 2026-1-5, which is no longer ISO.

Step 3: why the format is worth it

ISO text is always ten characters, and it sorts in date order as plain text:

Step 3

console.log(toIsoDate(fifth).length); // 10
console.log('2026-01-05' < '2026-12-25'); // true

Comparing strings works letter by letter, from the left. Because the year comes first and every part has a fixed width, the text order is the date order. You can sort a list of ISO dates with no date code at all. DD/MM/YYYY can't do that.

Step 4: the shortcut that changes the day

You may see this one-liner: take toISOString() and keep the first ten characters. toISOString() gives the full ISO date and time, but it always reads the date in UTC:

Step 4

const lateNight = new Date(2026, 0, 5, 0, 30); // 00:30 on 5 January, local
console.log(toIsoDate(lateNight));               // '2026-01-05'
console.log(lateNight.toISOString().slice(0, 10)); // '2026-01-04' in Lagos (UTC+1)

In Lagos, half past midnight on 5 January is 23:30 on 4 January in UTC, so the shortcut says the 4th. West of Greenwich, the same thing happens to late evenings, which come back as the next day. On a computer whose zone is UTC, the two agree, which is why this bug often only appears after the code goes live. toIsoDate reads local getters, so it always gives the date the reader sees on their own calendar.

The shortcut is fine when the date was built in UTC on purpose:

JavaScript

console.log(new Date(Date.UTC(2026, 0, 5)).toISOString().slice(0, 10)); // '2026-01-05'

The full solution

The panel has pad2, toIsoDate and a late-evening date printed both ways. Whether the two lines differ depends on the time zone of the machine running it. Press Run to see what yours does.

The answer

const pad2 = (n) => String(n).padStart(2, '0');

// Local getters, so the answer is the date the reader would call today.
function toIsoDate(date) {
  return date.getFullYear() + '-' + pad2(date.getMonth() + 1) + '-' + pad2(date.getDate());
}

const fifth = new Date(2026, 0, 5);     // 5 January 2026

console.log('5 January 2026  ->', toIsoDate(fifth));
console.log('25 December     ->', toIsoDate(new Date(2026, 11, 25)));

// The shortcut, and why it is not the same thing. toISOString() converts to UTC
// first, so east of Greenwich a late evening comes back as the NEXT day and
// west of it an early morning comes back as the previous one.
const evening = new Date(2026, 0, 5, 23, 30);
console.log('local  :', toIsoDate(evening));
console.log('via UTC:', evening.toISOString().slice(0, 10), '<- may differ, depending on the zone');

Output


      
7 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 7 of them and prints ok for each. That output is the proof, and the build fails if a single check fails.

7 checks

test('a single-digit month and day are padded', toIsoDate(fifth), '2026-01-05');
test('December needs no padding', toIsoDate(new Date(2026, 11, 25)), '2026-12-25');
test('always ten characters', toIsoDate(new Date(2026, 8, 2)).length, 10);
test('the month is shifted up by one', toIsoDate(new Date(2026, 1, 1)), '2026-02-01');
test('ISO text sorts chronologically, which is the point of it', toIsoDate(fifth) < toIsoDate(new Date(2026, 11, 25)), true);
test('and it survives a round trip through the parts', toIsoDate(new Date(2026, 0, 15)), '2026-01-15');
test('a UTC-built date agrees with toISOString', new Date(Date.UTC(2026, 0, 5)).toISOString().slice(0, 10), '2026-01-05');

Pad by hand and you get the local calendar date. Reach for toISOString().slice(0, 10) and you get the UTC calendar date, which is a different day for part of every 24 hours. This is the most common way a date drifts by one in a real application: someone in Lagos saves a task at 23:30 and the list shows it on tomorrow.

The sorting assertion is the reason ISO ordering is worth insisting on. '2026-01-05' < '2026-12-25' is true as plain text, so ISO dates sort correctly with no parsing at all. No other common format does.

Common mistakes

  • Forgetting + 1 on the month. January becomes 00 and every month is one behind.
  • Not padding. 2026-1-5 isn't ISO, and it no longer sorts correctly as text: '2026-10-01' < '2026-9-01' is true.
  • Using toISOString().slice(0, 10) for a local date. It reads the UTC date, which is a different day for some hours of every day in most zones.

Try it yourself

  1. Extend toIsoDate to add the time, so you get 2026-01-05T14:07.
  2. Sort ['2026-12-25', '2025-06-01', '2026-01-05'] with plain .sort() and check the order.
  3. Write fromIsoDate('2026-01-05') that splits the text on - and builds a local date with new Date(y, m - 1, d).

Keep practising

Newsletter

New writing, when it lands.

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