🗓️ Everything *[NYC] is back. A free gathering for AI builders. Sept 9 →

Best input type for year entry in Sanity schema

15 repliesLast updated: Nov 29, 2025

What would be the best route if I wanted an input in my document to be a year entry? Are there any helpers in Sanity for this? I tried searching
"year"
on the website and it isn't helpful when "year" is returning "learn" and "search" 😅 basically I want someone to be able to select a year from a dropdown or write a year in. However, I don't want to have to keep adding years and allowing just a string input seems dangerous in the sense that they could not enter a year
AI Update

For a year input field in Sanity, you have a couple of solid options depending on your needs:

Use a number field with validation rules to constrain the input to valid years:

defineField({
  name: 'year',
  title: 'Year',
  type: 'number',
  validation: (Rule) => Rule.required().min(1900).max(2100).integer()
})

This gives you:

You can adjust the min and max values to suit your needs. For example, if you only need recent years, you could set min(2000) and max(new Date().getFullYear() + 10).

Option 2: String field with a predefined list (dropdown)

If you want a dropdown with specific years, use a string field with an options.list:

defineField({
  name: 'year',
  title: 'Year',
  type: 'string',
  options: {
    list: [
      { title: '2025', value: '2025' },
      { title: '2024', value: '2024' },
      { title: '2023', value: '2023' },
      // ... more years
    ]
  }
})

The downside is you need to manually maintain the list. You could generate it programmatically in your schema:

const currentYear = new Date().getFullYear();
const years = Array.from({ length: 50 }, (_, i) => {
  const year = (currentYear - i).toString();
  return { title: year, value: year };
});

defineField({
  name: 'year',
  title: 'Year',
  type: 'string',
  options: { list: years }
})

Option 3: Date field (if you need full dates)

If you might eventually need more than just the year, you could use a date or datetime field and format it to show only the year in your frontend. However, this requires users to select a full date (like January 1st of that year), which might be awkward if you truly only care about the year.

My recommendation: Go with Option 1 (number field with validation) for the best balance of simplicity, data integrity, and user experience. It prevents invalid entries without requiring you to maintain a list of years that grows every January!

Show original thread
15 replies

Was this answer helpful?

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.

Related contributions