timeLast updated on
Last updated on
Validate time strings
Validate time strings in ISO 8601 format (HH:mm:ss). The Time primitive ensures times are properly formatted and represent valid clock times.
Examples
Creating times
// Direct validation
const time = open.time('14:30:00')
// Parse unknown input
const time = open.time.parse(jsonData)
// Get current time
const now = open.time.now()
// Check validity without throwing
if (open.time.isValid('23:59:59')) {
console.log('Valid time')
}Parsing external data
// Parse unknown input (throws on error)
const time = open.time.parse(userInput)
// Safe parsing (returns result object)
const result = open.time.safeParse(userInput)
if (result.success) {
console.log(`Time: ${result.data}`)
} else {
console.error(result.error.message)
}Using with forms
const form = open.form({
name: 'schedule',
fields: {
startTime: { type: 'time', label: 'Start Time' },
endTime: { type: 'time', label: 'End Time' }
}
})
const filled = form.fill({
fields: {
startTime: open.time('09:00:00'),
endTime: open.time('17:00:00')
}
})API
Direct Call
open.time(input: string): stringPass a time string for validation. Returns the validated string or throws on error.
const time = open.time('14:30:00')Static Methods
parse: (input: unknown) => string
Parse unknown input (throws on error)
safeParse: (input: unknown) => Result<string>
Parse unknown input (returns result object)
isValid: (input: string) => boolean
Check if a string is a valid time
now: () => string
Get current time as ISO string
Format
The Time primitive expects and returns strings in ISO 8601 time format:
HH:mm:ss
HH:mm:ss.sss| Component | Description | Range |
|---|---|---|
HH | Hours (24-hour) | 00-23 |
mm | Minutes | 00-59 |
ss | Seconds | 00-59 |
.sss | Milliseconds (optional) | 000-999 |
Common Times
| Time | Description |
|---|---|
00:00:00 | Midnight |
06:00:00 | 6:00 AM |
12:00:00 | Noon |
13:00:00 | 1:00 PM |
18:00:00 | 6:00 PM |
23:59:59 | End of day |
Validation
The Time primitive validates:
- String must match
HH:mm:ssorHH:mm:ss.sssformat - Hours must be 00-23
- Minutes must be 00-59
- Seconds must be 00-59
// Valid
open.time('00:00:00')
open.time('12:30:00')
open.time('23:59:59')
open.time('14:30:00.123')
// Invalid - throws Error
open.time('24:00:00')
// Error: Invalid Time: hours must be between 00 and 23
open.time('12:60:00')
// Error: Invalid Time: minutes must be between 00 and 59
open.time('14:30')
// Error: Invalid Time: must be in HH:mm:ss format
open.time('2:30:00')
// Error: Invalid Time: must be in HH:mm:ss format
open.time('not-a-time')
// Error: Invalid Time: must be in HH:mm:ss formatRelated
- Primitives Overview - All available primitives
- Date - Date only
- Datetime - Combined date and time
- Time Field - Time field type