Hour in 24-hour format (normalized to 0-23 range)
Formatted time label in 12-hour format with AM/PM
export function getHourLabel(hour: number): string {
// Handle negative hours and hours > 23 properly by applying modulo 24
// JavaScript modulo doesn't work with negative numbers the way we need
// so we add 24 first for negative numbers
const normalizedHour = ((hour % 24) + 24) % 24;
const period = normalizedHour >= 12 ? "PM" : "AM";
const displayHour = normalizedHour % 12 || 12; // Convert 0 to 12 for 12 AM
return `${displayHour} ${period}`;
}
Formats 24-hour time value to 12-hour format with AM/PM
Converts raw hour values (0-23) to user-friendly time labels for time-based chart axes and temporal data display. Handles edge cases like midnight and noon with proper formatting.
Supports cyclic hour values and normalizes any hour value to the correct 0-23 range with modulo arithmetic.
Examples: