Extract Total Minutes
getTotalMinutes
Converts a time string in HH:MM
or -HH:MM
format into total minutes from 00:00
.
Function Signature
function getTotalMinutes(time: `-${Time}` | Time): number;
Parameters
time
: A time string in either:- Positive format:
HH:MM
(e.g.,"08:30"
) - Negative format:
-HH:MM
(e.g.,"-05:00"
)
- Positive format:
Returns
Total minutes elapsed since 00:00
:
- Positive number for times in
HH:MM
format - Negative number for times in
-HH:MM
format 0
for00:00
or-00:00
Example Usage
import { getTotalMinutes } from 'nhb-toolbox';
// Positive time
console.log(getTotalMinutes("14:45")); // 885 (14*60 + 45)
console.log(getTotalMinutes("01:30")); // 90
// Negative time
console.log(getTotalMinutes("-03:15")); // -195
console.log(getTotalMinutes("-00:30")); // -30
// Edge cases
console.log(getTotalMinutes("00:00")); // 0
console.log(getTotalMinutes("-00:00")); // 0
Notes
- Handles both positive and negative time formats
- Maintains sign convention from input
- Returns raw numeric value (no rounding)
- Follows 24-hour time format
- Returns 0 for both
00:00
and-00:00
- It internally uses extractHourMinute
Aliases
extractTotalMinutesFromTime
getTotalMinutesFromTime
Type Safety
Input is strictly validated through types:
type Time = `${Hours}:${Minutes}`;
type Hours = '00'|'01'|...|'23';
type Minutes = '00'|'01'|...|'59';
Use Cases
- Time duration calculations
- Timezone offset conversions
- Scheduling and time arithmetic
- Comparing time intervals
- Converting time formats for storage
Conclusion
The getTotalMinutes
function provides:
- Simple conversion from time strings to minutes
- Accurate time math for calculations
- Consistent handling of signed times
- Type-safe operations through strict input validation
Ideal for applications requiring:
- Time-based calculations
- Duration measurements
- Timezone conversions
- Scheduling logic
- Time arithmetic operations