Time Guards
isValidTime
Type guard that checks if a value is a valid time string in "HH:MM" format.
Function Signature
function isValidTime(value: unknown): value is Time;
Parameters
value
: The value to check (any type)
Returns
true
if the value is a valid 24-hour time string, false
otherwise
Example Usage
import { isValidTime } from 'nhb-toolbox';
console.log(isValidTime("14:30")); // true
console.log(isValidTime("25:00")); // false (invalid hour)
console.log(isValidTime("12:60")); // false (invalid minute)
console.log(isValidTime(1234)); // false (not a string)
Validation Rules
- Must be a string in "HH:MM" format
- Hours must be between 00-23
- Minutes must be between 00-59
- Leading zeros required for single-digit values
Alias
isValidTimeString
Type Definition
type ClockTime = `${ClockHour}:${ClockMinute}`;
type ClockHour = '00'|'01'|...|'23';
type ClockMinute = '00'|'01'|...|'59';
isValidUTCOffSet
Type guard that checks if a value is a valid UTC offset string.
Function Signature
function isValidUTCOffSet(value: unknown): value is UTCOffSet;
Parameters
value
: The value to check (any type)
Returns
true
if the value is a valid UTC offset string, false
otherwise
Example Usage
import { isValidUTCOffSet } from 'nhb-toolbox';
console.log(isValidUTCOffSet("UTC+05:30")); // true
console.log(isValidUTCOffSet("UTC-14:00")); // true
console.log(isValidUTCOffSet("GMT+08:00")); // false (invalid prefix)
console.log(isValidUTCOffSet("UTC+25:00")); // false (invalid hour)
Validation Rules
- Must start with "UTC" prefix
- Must be followed by + or - sign
- Hours must be 1-2 digits
- Must include colon separator
- Minutes must be exactly 2 digits
- Followed by valid time components (hours 00-14, minutes 00/15/30/45)
Alias
isValidUTC
Type Definition
type UTCOffSet = `UTC${PositiveUTCHour|NegativeUTCHour}:${UTCMinute}`;
Conclusion
These type guards provide:
- Strict validation of time formats
- Type narrowing for TypeScript
- Consistent checking across applications
- Safety for time-related operations
Ideal for:
- Input validation
- API response checking
- Type-safe time operations
- Data sanitization