Add AM PM suffix to an hour

Turn an hour from 0 to 23 into a 12-hour label with am or pm.

JavaScript Version 1.0.0 · MIT · .md

add-am-pm-suffix-to-an-hour.js
function suffixAmPm(hour) { return `${hour % 12 === 0 ? 12 : hour % 12}${hour < 12 ? 'am' : 'pm'}`; }

Input & expected output

Input

15

Expected output

"3pm"

The edge case

Input

0

Expected output

"12am"

Know the limits

  • Input is an integer hour from 0 to 23.
  • For text a reader sees, prefer Intl.DateTimeFormat in the reader’s locale.

The function above is the implementation used by the lab. The lab validates JSON types and caps size before execution; include equivalent validation when accepting untrusted input in your application.

Source & maintenance

Based on Add AM PM suffix to an hour from the original 1loc collection by Nguyen Huu Phuoc. Copyright (c) 2020 phuoc-ng. MIT licensed.

Original implementation kept; integer 0–23 validation and locale note. Original MDX retained as inert source evidence.

Maintained by Oveyon. Source commit: 14b481cf60df. Read the license.

Local fixture scope · Node.js 22.23.2 · reviewed October 28, 2026. No external service is required.

Keep exploring

Type two or more characters. Try “duplicate”, “webhook” or “retry”.