OpenForm

time

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): string

Pass 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
ComponentDescriptionRange
HHHours (24-hour)00-23
mmMinutes00-59
ssSeconds00-59
.sssMilliseconds (optional)000-999

Common Times

TimeDescription
00:00:00Midnight
06:00:006:00 AM
12:00:00Noon
13:00:001:00 PM
18:00:006:00 PM
23:59:59End of day

Validation

The Time primitive validates:

  • String must match HH:mm:ss or HH:mm:ss.sss format
  • 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 format

Related

On this page