Custom time-only input field in Sanity schemas

3 replies
Last updated: Jun 23, 2024
Hey community, I was wondering, there are the
Date and Datetime SchemaTypes. What about time only? Yes, there is the great Availability Plugin but in my localized project I need to control the output of time format which I could not solve. Anyone experienced to create a custom time only input? Thanks a lot!!!
AI Update

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:

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 thread
3 replies
I used this one for mine: https://www.sanity.io/guides/create-a-time-duration-object-field
It worked pretty well and while I didn't need duration every time it allowed for time as a string I believe which could then be added to a datetime field if needed.
it should at least give you the framework to modify this one to what you might need.
Yeah, Thank you, looks good to me, just need to loacalize the output.

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.

Was this answer helpful?