Format a date as DD/MM/YYYY in JavaScript
Beginner · 3 min read
In Nigeria, Britain and most of the world, 5 January 2026 is written 05/01/2026: day first, then month, then year. In the United States, the same day is written 01/05/2026, month first. In this tutorial you will build the day-first form from a JavaScript date, then the month-first form from the same parts, and see why mixing them up is a real problem and not a small one.
You need to know how to make a date with new Date(year, month, day), that getMonth() counts from 0, and how padStart adds leading zeros.
The same three parts, two orders
Both formats use exactly the same pieces:
dd, the day of the month, two digits;mm, the month, two digits;yyyy, the year, four digits.
Only the order changes. So a good plan is to build the three pieces once, and then write two tiny functions that arrange them.
Returning an object
A function can return several values at once by putting them in an object, with a name for each one:
function sizes() {
return { small: 1, large: 9 };
}
const s = sizes();
console.log(s.small, s.large); // 1 9
You will use this to hand back dd, mm and yyyy together.
Here is the exercise. Have a go yourself first if you like, then read on and we will build it together.
6 Format a date as DD/MM/YYYY
easyProduce 05/01/2026 for 5 January 2026 — day first, the British and most-of-the-world order. Then produce the American order from the same parts and compare them.
Stuck? Three hints, smallest first
- 1. Build the three padded parts once and then arrange them.
- 2. Only the order changes between the two formats, not the values.
- 3. Pick a date where the day and month are both under 13 to see why the ambiguity matters.
Step 1: a padding helper
The day and month need two digits, so 5 must become 05:
const pad2 = (n) => String(n).padStart(2, '0');
console.log(pad2(5), pad2(12)); // 05 12
String(n) turns the number into text, and padStart(2, '0') adds zeros to the front until it is two characters long.
Step 2: build the parts once
function parts(date) {
return {
dd: pad2(date.getDate()),
mm: pad2(date.getMonth() + 1),
yyyy: String(date.getFullYear()),
};
}
const fifthOfJanuary = new Date(2026, 0, 5);
console.log(parts(fifthOfJanuary)); // { dd: '05', mm: '01', yyyy: '2026' }
getMonth() gives 0 for January, so add 1 before padding. All three parts are strings, ready to be joined.
Step 3: day first
const dayFirst = (date) => { const p = parts(date); return p.dd + '/' + p.mm + '/' + p.yyyy; };
console.log(dayFirst(fifthOfJanuary)); // '05/01/2026'
The arrow function has curly braces this time, because it does two things: it gets the parts, then returns the joined text. With braces, you need the word return.
Step 4: month first
Swap the first two parts:
const monthFirst = (date) => { const p = parts(date); return p.mm + '/' + p.dd + '/' + p.yyyy; };
console.log(monthFirst(fifthOfJanuary)); // '01/05/2026'
Because both functions share parts, a fix to the padding or the month only has to be made once.
Step 5: see the collision
Now format 1 May 2026 (month 4) day first, and compare it with 5 January month first:
const firstOfMay = new Date(2026, 4, 1);
console.log(dayFirst(firstOfMay)); // '01/05/2026'
console.log(monthFirst(fifthOfJanuary) === dayFirst(firstOfMay)); // true
The two strings are identical. 01/05/2026 means 5 January to a reader in New York and 1 May to a reader in Lagos, and nothing in the text says which one you meant. That is a real risk for anything with a deadline, a delivery or a booking.
Some dates are safe. 25 December gives 25/12/2026, and since there is no month 25, nobody can misread it. The problem is any date where both the day and the month are 12 or less. That is 12 times 12, so 144 days of every year.
What to do in a real app
Store and send dates as ISO text, 2026-01-05, which has only one reading. Turn them into dd/mm/yyyy or mm/dd/yyyy only at the last moment, when you show the date to a person and know which order they expect.
The full solution
The panel has pad2, parts and both formatters, with checks that include the January and May collision. Press Run to see them.
const pad2 = (n) => String(n).padStart(2, '0');
function parts(date) {
return {
dd: pad2(date.getDate()),
mm: pad2(date.getMonth() + 1),
yyyy: String(date.getFullYear()),
};
}
const dayFirst = (date) => { const p = parts(date); return p.dd + '/' + p.mm + '/' + p.yyyy; };
const monthFirst = (date) => { const p = parts(date); return p.mm + '/' + p.dd + '/' + p.yyyy; };
const fifthOfJanuary = new Date(2026, 0, 5);
console.log('5 January 2026, day first :', dayFirst(fifthOfJanuary));
console.log('5 January 2026, month first:', monthFirst(fifthOfJanuary));
console.log('the same eight characters mean two different days');
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('day first, as written in Britain', dayFirst(fifthOfJanuary), '05/01/2026');
test('month first, as written in the States', monthFirst(fifthOfJanuary), '01/05/2026');
test('both are eight digits and two slashes', dayFirst(fifthOfJanuary).length, 10);
test('25 December is unambiguous, because 25 cannot be a month', dayFirst(new Date(2026, 11, 25)), '25/12/2026');
test('and reversing it is still readable', monthFirst(new Date(2026, 11, 25)), '12/25/2026');
test('but 5 January and 1 May swap into each other', monthFirst(fifthOfJanuary), dayFirst(new Date(2026, 4, 1)));
test('which is a real collision, not a contrived one', dayFirst(new Date(2026, 4, 1)), '01/05/2026');
test('the parts themselves never change, only their order', parts(fifthOfJanuary), { dd: '05', mm: '01', yyyy: '2026' });
The sixth assertion is the whole exercise: 01/05/2026 is 5 January in London and 1 May in New York, and nothing in the string says which. For any date where both numbers are 12 or less — that is 144 days a year — the format is genuinely ambiguous.
So: store and transmit ISO 2026-01-05, and format day-first or month-first only at the point of display, where you know who is reading. Everything in this section that has to be pinned down uses ISO or DD/MM, and says so.
Common mistakes
- Forgetting
+ 1on the month. 5 January would print as05/00/2026. - Not padding.
5/1/2026is harder to read and the length changes from date to date, which breaks columns and fixed-width layouts. - Saving the display format. If you store
01/05/2026in a database, you can't tell later which order it was in. Store ISO and format when you display.
Try it yourself
- Write
dayFirstDashes(date)that gives05-01-2026. - Write
isAmbiguous(date)that returnstruewhen both the day and the month are 12 or less. - Count how many days in 2026 are ambiguous by looping through every day. Do you get 144?