Extract Hour & Minute
Extract Hour & Minute from Time — Chronos Documentation.
extractHourMinute
Extracts the hour and minute components from a time string in HH:MM or -HH:MM format and returns them as a numeric tuple.
Import
import { extractHourMinute } from "toolbox-x/utils";Function Signature
extractHourMinute(time: `-${ClockTime}` | ClockTime): [number, number];Parameters
time: A string in either:- Positive format:
HH:MM(e.g.,"08:30") - Negative format:
-HH:MM(e.g.,"-05:00")
- Positive format:
Return Value
A tuple containing:
- Hour component (number)
- Minute component (number)
Example Usage
playground.ts
Notes
- Handles both positive and negative time formats
- Returns raw numbers (negative sign preserved for hour)
- Uses strict type checking for valid time formats
- Follows 24-hour time format conventions
- Minute component always positive (0-59)
Type Safety
The input type ensures only valid formats are accepted:
type ClockTime = `${ClockHour}:${ClockMinute}`; // HH:MM format
type ClockHour = '00'|'01'|...|'23'; // Valid hours
type ClockMinute = '00'|'01'|...|'59'; // Valid minutesEdge Cases
"00:00"returns[0, 0]"-00:30"returns[0, 30](negative zero becomes zero)"23:59"returns[23, 59]
Use Cases
- Time manipulation utilities
- Timezone offset calculations
- Duration parsing
- Scheduling applications
- Time-based validation
Conclusion
The extractHourMinute function provides:
- Strict input validation through types
- Simple time component extraction
- Consistent numeric output
- Flexible handling of signed times
Last updated: Fri, May 22, 2026 07:46:19AM (Coordinated Universal Time)
