Emmanuel Akinfulubi

JavaScriptDate

Format time in 12-hour AM/PM in JavaScript

Beginner · 3 min read

JavaScript keeps time on a 24-hour clock, so quarter to two in the afternoon is hour 13. Many people would rather read 1:45 PM. In this tutorial you will write a function called clock12 that turns any date into that 12-hour form, with AM or PM on the end.

Most of it is simple arithmetic. The two hours that catch people are midnight and noon, and you will see exactly why the obvious code gets both of them wrong. You need to know how to make a date with a time and how padStart adds leading zeros.

How the 12-hour clock works

The day is split into two halves:

  • AM runs from midnight up to just before noon. Midnight itself is written 12:00 AM, not 0:00 AM.
  • PM runs from noon up to just before midnight. Noon is 12:00 PM.

So the hour shown is always from 1 to 12. There is never an hour 0, and there is never an hour 13.

The remainder operator

% gives the remainder after dividing. Dividing an hour by 12 and keeping the remainder folds the afternoon back onto the morning:

JavaScript

console.log(13 % 12); // 1
console.log(23 % 12); // 11
console.log(12 % 12); // 0
console.log(0 % 12);  // 0

The first two are what you want. The last two are the problem you will fix.

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

7 A 12-hour clock with AM and PM

easy

Show the time as 1:45 PM. Midnight and noon are the two cases that catch people.

Stuck? Three hints, smallest first
  1. 1. hours % 12 maps 13 to 1, which is what you want — until you try it on 0 and 12.
  2. 2. Both midnight and noon should read 12, so the remainder 0 needs a fallback.
  3. 3. Noon is PM. The boundary is hours < 12, not hours <= 12.

Step 1: a first attempt

Read the hour with getHours(), fold it with % 12, and pick the suffix:

Step 1

const pad2 = (n) => String(n).padStart(2, '0');
const at = (h, m) => new Date(2026, 0, 15, h, m);

function clock12(date) {
  const h24 = date.getHours();
  const suffix = h24 <= 12 ? 'AM' : 'PM';
  return (h24 % 12) + ':' + pad2(date.getMinutes()) + ' ' + suffix;
}
console.log(clock12(at(13, 45))); // '1:45 PM'
console.log(clock12(at(0, 5)));   // '0:05 AM'
console.log(clock12(at(12, 0)));  // '0:00 AM'

at is a small helper that builds a time on 15 January 2026, so each example is one short line. The afternoon works. Midnight shows 0:05, and noon is wrong twice: the hour is 0 and the suffix says AM.

Step 2: fix the suffix

Noon starts the PM half, so the morning is every hour less than 12, not "less than or equal to":

Step 2

const suffixFor = (h24) => (h24 < 12 ? 'AM' : 'PM');
console.log(suffixFor(11), suffixFor(12), suffixFor(0)); // AM PM AM

Hour 11 is still morning, hour 12 is the afternoon, and hour 0 (midnight) is morning.

Step 3: fix the hour

h24 % 12 is 0 for both midnight and noon, and both should show 12. So when the remainder is 0, use 12 instead:

Step 3

const hourFor = (h24) => (h24 % 12 === 0 ? 12 : h24 % 12);
console.log(hourFor(0), hourFor(12), hourFor(13), hourFor(9)); // 12 12 1 9

The ? : is the conditional operator: "if the test is true, use the first value, otherwise the second".

Notice that midnight and noon both turn into 12. That is why the suffix must be worked out from the original 24-hour value. Once the hour has become 12, you can no longer tell the two apart.

Step 4: put it together

Step 4

function clock12(date) {
  const h24 = date.getHours();
  const suffix = h24 < 12 ? 'AM' : 'PM';
  const h12 = h24 % 12 === 0 ? 12 : h24 % 12;
  return h12 + ':' + pad2(date.getMinutes()) + ' ' + suffix;
}
console.log(clock12(at(0, 5)));   // '12:05 AM'
console.log(clock12(at(12, 0)));  // '12:00 PM'
console.log(clock12(at(12, 30))); // '12:30 PM'
console.log(clock12(at(9, 7)));   // '9:07 AM'
console.log(clock12(at(23, 59))); // '11:59 PM'

The minutes are padded, so 7 minutes shows as 07. The hour is not padded, which matches how 12-hour times are usually written: 9:07, not 09:07.

The full solution

The panel runs clock12 over six times across the day, then prints the current time from the live clock (shown, never tested). Press Run and look at the midnight and noon rows.

The answer

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

function clock12(date) {
  const h24 = date.getHours();
  const suffix = h24 < 12 ? 'AM' : 'PM';        // 12:00 is PM, so < and not <=
  const h12 = h24 % 12 === 0 ? 12 : h24 % 12;   // 0 and 12 both display as 12
  return h12 + ':' + pad2(date.getMinutes()) + ' ' + suffix;
}

// Six times on one day, 15 January 2026.
const at = (h, m) => new Date(2026, 0, 15, h, m);

for (const [h, m] of [[0, 5], [9, 7], [12, 0], [12, 30], [13, 45], [23, 59]]) {
  console.log(pad2(h) + ':' + pad2(m), '->', clock12(at(h, m)));
}
console.log('right now:', clock12(new Date()));   // shown, not asserted

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('just after midnight is 12 AM, not 0 AM', clock12(at(0, 5)), '12:05 AM');
test('noon is 12 PM, not 12 AM', clock12(at(12, 0)), '12:00 PM');
test('and half past noon is still PM', clock12(at(12, 30)), '12:30 PM');
test('the afternoon loses twelve hours', clock12(at(13, 45)), '1:45 PM');
test('the last minute of the day', clock12(at(23, 59)), '11:59 PM');
test('the hour is not padded, the minute is', clock12(at(9, 7)), '9:07 AM');
test('no hour ever displays as 0', clock12(at(0, 0)).startsWith('12'), true);
test('11 AM is the last AM hour', clock12(at(11, 59)), '11:59 AM');

hours % 12 is right for eleven hours out of twelve and wrong for the two that matter. It gives 0 at midnight and at noon, so both need the fallback to 12 — and they need different suffixes, which is why the AM/PM test is on h24 and not on the converted hour.

The other half-mistake is h24 <= 12 ? 'AM' : 'PM', which labels noon as AM. The 12:00 and 12:30 assertions exist to catch exactly that.

Common mistakes

  • Using <= 12 for AM. Noon comes out as 12:00 AM. Noon is PM, so the test is < 12.
  • Showing h24 % 12 as it is. Midnight and noon both become 0. Swap 0 for 12.
  • Working out AM or PM from the converted hour. After the swap, midnight and noon are both 12, so the suffix must come from the 24-hour value.

Try it yourself

  1. Add seconds, so the output looks like 1:45:09 PM.
  2. Write the reverse: to24('1:45 PM') returns '13:45'. Test it on '12:05 AM' and '12:00 PM'.
  3. Write isMorning(date) that returns true for AM times.

Keep practising

Newsletter

New writing, when it lands.

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