Emmanuel Akinfulubi

JavaScriptBasic

Format a date as mm-dd-yyyy and dd/mm/yyyy in JavaScript

Beginner · 3 min read

The same day can be written many ways. In the United States, 2 September 2026 is usually written 09/02/2026, month first. In Nigeria, the UK and much of the world it is 02/09/2026, day first. Forms, receipts and reports often need one of these, so turning a date into a fixed pattern is a job you will do a lot.

In this tutorial you will write a function called formats. You give it a date, and it returns an object holding the date in four patterns: mm-dd-yyyy, mm/dd/yyyy, dd-mm-yyyy and dd/mm/yyyy. You need to know what a function and an object are. The date methods are explained below.

Reading the parts of a date

A Date object has a method for each part. Three of them are all you need here:

JavaScript

const d = new Date(2026, 8, 2);  // 2 September 2026
console.log(d.getFullYear());    // 2026
console.log(d.getMonth());       // 8
console.log(d.getDate());        // 2

Look at the month. September is the ninth month, but getMonth() says 8. Months count from 0, so January is 0 and December is 11. Days of the month count from 1 as you would expect. This mismatch causes more date bugs than anything else, so remember it: add 1 to the month.

Also note the name: getDate() gives the day of the month. The similar-looking getDay() gives the day of the week, which is a different thing.

Two digits, always

In "mm" and "dd", each letter stands for one digit, so the month and day always use two. January is 01, not 1. The string method padStart(2, '0') adds zeros to the front until the text is 2 characters long:

JavaScript

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

Here is the exercise. Have a go yourself first if you like, then follow along.

3 The same date in four formats

easy

Take a date and produce it as mm-dd-yyyy, mm/dd/yyyy, dd-mm-yyyy and dd/mm/yyyy.

Stuck? Three hints, smallest first
  1. 1. getMonth() is zero-based, so January is 0. Add one.
  2. 2. Pad the month and day to two digits with padStart(2, '0').
  3. 3. Build the parts once, then join them in the four orders rather than repeating the work.

Step 1: get the three parts

Read the year, month and day. Remember the + 1.

Step 1

const date = new Date(2026, 8, 2);
const yyyy = date.getFullYear();
const mm = date.getMonth() + 1;
const dd = date.getDate();
console.log(yyyy, mm, dd); // 2026 9 2

Without the + 1 you would print August's number for a September date.

Step 2: pad them

padStart is a string method, and these are numbers. A small helper turns a number into a string, then pads it:

Step 2

const pad = (n) => String(n).padStart(2, '0');
console.log(pad(9));  // 09
console.log(pad(2));  // 02
console.log(pad(11)); // 11

The year already has four digits, so it doesn't need padding.

Step 3: join the parts in each order

You could write each pattern by hand with +. It is tidier to put the parts in an array in the order you want, then join them with the separator. join('-') puts a dash between each item.

Step 3

const parts = ['09', '02', 2026];
console.log(parts.join('-')); // 09-02-2026
console.log(parts.join('/')); // 09/02/2026

Swap the first two items and you have the day-first patterns.

Step 4: one function, four patterns

Now put it together. The parts are worked out once at the top, and each pattern only arranges them.

Step 4

function formats(date) {
  const pad = (n) => String(n).padStart(2, '0');
  const yyyy = date.getFullYear();
  const mm = pad(date.getMonth() + 1);
  const dd = pad(date.getDate());
  return {
    'mm-dd-yyyy': [mm, dd, yyyy].join('-'),
    'mm/dd/yyyy': [mm, dd, yyyy].join('/'),
    'dd-mm-yyyy': [dd, mm, yyyy].join('-'),
    'dd/mm/yyyy': [dd, mm, yyyy].join('/'),
  };
}

const out = formats(new Date(2026, 8, 2));
console.log(out['mm-dd-yyyy']); // 09-02-2026
console.log(out['dd/mm/yyyy']); // 02/09/2026

The keys have dashes and slashes in them, so they need quotes, and you read them with square brackets instead of a dot.

Now test a date where both the day and the month have a single digit:

JavaScript

console.log(formats(new Date(2026, 0, 5))['dd/mm/yyyy']); // 05/01/2026

The full solution

The complete answer loops over all four patterns and prints each one, then prints 5 January to show the padding. Press Run to see it.

The answer

function formats(date) {
  const pad = (n) => String(n).padStart(2, '0');

  const yyyy = date.getFullYear();
  const mm = pad(date.getMonth() + 1);   // zero-based, so +1
  const dd = pad(date.getDate());        // getDate is the day of the month

  // Build the pieces once, then arrange them.
  return {
    'mm-dd-yyyy': [mm, dd, yyyy].join('-'),
    'mm/dd/yyyy': [mm, dd, yyyy].join('/'),
    'dd-mm-yyyy': [dd, mm, yyyy].join('-'),
    'dd/mm/yyyy': [dd, mm, yyyy].join('/'),
  };
}

const out = formats(new Date(2026, 8, 2));   // 2 September 2026
for (const [label, value] of Object.entries(out)) console.log(label, '->', value);

// A single-digit day and month, to prove the padding.
const padded = formats(new Date(2026, 0, 5));
console.log('5 January:', padded['dd/mm/yyyy']);

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.

6 checks

test('month first with dashes', out['mm-dd-yyyy'], '09-02-2026');
test('month first with slashes', out['mm/dd/yyyy'], '09/02/2026');
test('day first with dashes', out['dd-mm-yyyy'], '02-09-2026');
test('day first with slashes', out['dd/mm/yyyy'], '02/09/2026');
test('single digits padded', padded['dd/mm/yyyy'], '05/01/2026');
test('all four formats returned', Object.keys(out).length, 4);

getMonth() counting from zero while getDate() counts from one is one of the oldest inconsistencies in the language, and it is the single most common date bug. Note also that 09-02-2026 and 02-09-2026 are the same day: this is exactly why an API should send ISO 2026-09-02 and let the display layer decide.

Common mistakes

  • Forgetting to add 1 to the month. Every date comes out one month early, and January shows as 00.
  • Using getDay() instead of getDate(). You get the day of the week (0 to 6), not the day of the month.
  • Calling padStart on a number. Numbers don't have it. Convert with String(n) first.

Try it yourself

  1. Add a fifth pattern, yyyy-mm-dd. This is the international standard (ISO 8601), and it sorts correctly as plain text.
  2. Write formatAs(date, pattern) that takes a pattern like 'dd.mm.yyyy' and replaces dd, mm and yyyy in it.
  3. Print 09-02-2026 and 02-09-2026 side by side. Can you tell which is which without knowing the pattern? That is why APIs send yyyy-mm-dd.

Keep practising

Newsletter

New writing, when it lands.

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