Custom time-only input field in Sanity schemas
You're right that Sanity doesn't have a built-in time-only schema type! While there are date and datetime types, you'll need to create a custom input component for time-only fields, especially if you need control over localization and formatting.
Here are a few approaches you can take:
1. Custom Input Component (Recommended for Localization Control)
Since you need to control the time format output in your localized project, building a custom input component gives you the most flexibility. You can store the time as a string in your preferred format (like "HH:mm" or "HH:mm:ss") and create a custom time picker interface.
Here's a basic example structure:
import {set, unset} from 'sanity'
export function TimeInput(props) {
const {onChange, value} = props
const handleChange = (event) => {
const timeValue = event.currentTarget.value
onChange(timeValue ? set(timeValue) : unset())
}
return (
<input
type="time"
value={value || ''}
onChange={handleChange}
/>
)
}Then in your schema:
{
name: 'openingTime',
type: 'string',
components: {
input: TimeInput
}
}This gives you full control over formatting and localization since you're handling the string value directly. You can add formatting logic, validation, and localization as needed.
2. Community Plugins
There are a few time picker plugins in the Sanity ecosystem that you can search for in the Sanity plugin directory. However, as you've noted with the Availability Plugin, these may not give you the localization control you need.
3. Store as String with Validation
The simplest approach might be to use a regular string type with validation to ensure proper time format:
{
name: 'time',
type: 'string',
validation: Rule => Rule.regex(/^([0-1]?[0-9]|2[0-3]):[0-5][0-9]$/, {
name: 'time',
invert: false
}).error('Must be a valid time in HH:mm format')
}Then add your custom input component for the UI while maintaining full control over the stored format.
The custom input component route is definitely your best bet for localization control. You can format the display however you need for your locale while storing it in a consistent format in your Content Lake. Check out the guide on creating your first input component for Sanity Studio v3 for more detailed implementation steps!
Show original thread3 replies
Sanity – Build the way you think, not the way your CMS thinks
Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.