How to handle timezones in Sanity? Looking for date + time + timezone plugin

10 replies
Last updated: May 6, 2022
Hi, long time that I've not been there 👋I'm looking for a plugin that handles time zones and I'm a bit surprised to see that doesn't exist
🧐I've seen the
richDate
type but as already reported in an issue , the plugin is a bit incomplete. Does anyone know a plugin that I haven't found? Or does someone have an easy way to handle time zones?My need is to set a date + hour + timezone. From what I see so far, I'll have to built it myself.
AI Update

You're right to be surprised – there isn't a dedicated timezone plugin for Sanity, and that's because Sanity's datetime field doesn't actually store timezone information. It only stores UTC timestamps.

The allowTimeZoneSwitch and displayTimeZone options you might have seen in the datetime type only control how the datetime is displayed in the Studio UI – they don't capture or store which timezone an event actually belongs to. This is a common point of confusion.

The Reality: You'll Need to Build It

For your use case (storing date + time + timezone), you'll need to create a custom solution. Here are two practical approaches:

Option 1: Object with Datetime + Timezone String

Create a custom object type that combines a datetime field with a separate timezone field:

{
  name: 'scheduledEvent',
  type: 'object',
  fields: [
    {
      name: 'datetime',
      type: 'datetime',
      title: 'Date and Time'
    },
    {
      name: 'timezone',
      type: 'string',
      title: 'Timezone',
      options: {
        list: [
          {title: 'Eastern Time', value: 'America/New_York'},
          {title: 'Central Time', value: 'America/Chicago'},
          {title: 'Mountain Time', value: 'America/Denver'},
          {title: 'Pacific Time', value: 'America/Los_Angeles'},
          {title: 'UTC', value: 'UTC'},
          // Add more IANA timezone identifiers as needed
        ]
      }
    }
  ]
}

This stores both pieces of information separately, which you can then combine in your frontend using libraries like date-fns-tz or luxon.

Option 2: Store as ISO String with Offset

Alternatively, you could use a string field and store the complete ISO 8601 string with timezone offset (like 2024-03-15T14:30:00-04:00), but this requires more custom input handling in Studio.

Why richDate Isn't the Answer

The richDate plugin you mentioned was an attempt to improve the datetime input experience, but as you've discovered from the issues, it doesn't solve the timezone storage problem either. It's more focused on flexible date/time input formats than timezone handling.

The Path Forward

Since this is a common need (events, scheduling, etc.), building a reusable custom object type like Option 1 is your best bet. You could even package it as a plugin and share it with the community – there's clearly a gap here that others would benefit from!

The key is understanding that Sanity gives you the primitives (datetime for the timestamp, string for the timezone identifier), but combining them into a cohesive "datetime with timezone" experience is something you'll need to implement in your schema design and potentially with a custom input component if you want a polished UI.

Show original thread
10 replies
I’m using a simple field with a list of options and deal with time zone on the frontend.
I did that on several sites and it’s working well. I’m not sure to understand why you'd want to handle time zones directly in Sanity?
What do you mean by "a list of options"? Are you listing time zones?My use case is that the editor has datetime to set with the time zone to handle. For example he might want to set a time at 4pm for the UTC+2 time zone and another one at 6pm on UTC-6.
I use something like this:
 {
   name: 'timeZone',
   type: 'string',
   options: { list: [...timeZones] }
 }

export const timeZones = [
 'America/Adak',
 'America/Anchorage',
 'America/Anguilla',
 'America/Antigua',
 'America/Araguaina',
 'America/Argentina/Buenos_Aires',
 'America/Argentina/Catamarca',
 'America/Argentina/Cordoba',
 ...
]
And I handle the rest on the frontend

{date.toLocaleTimeString('en-US', { timeZone })}
It’s working fine for simple use cases
As the datetime field is computed using the browser's locale time zone, isn't this causing problem?
For my usage it was fine, but I only needed to display hours on a footer, so not that important 😅
I’ve been pondering this for a bit. I still want an ISO UTC date to appear in a query, but I want to be sure that users are consistently presented with a clear timezone/offset. The datetime type default input only provides UTC or relative offset options. You might be able to extend momentjs with timezones I can figure out nice javascript methods or make my own, but it’s turning this into an intuitive ui that has me a little stumped. I’ve been browsing bigger UI libraries like MUI and Mantine for something but haven’t quite found what I’m looking for. I could create a set of inputs to handle this and then insert an ISO date - but this doesn’t feel like a very elegant solution.
I’d love to hear how others have solved this.
Reminder that momentjs core and styled components are built in, so if you find any UI tools that rely on these, they can be added to your studio build in a custom component with very little additional effort.
After some research, I found that momentjs would be the best solution + it's already in Studio.My idea for now is to use a custom datetimeTimeZone field with a datetime picker + a time zone picker (list pulled from moment-timezone). At the saving time, the field would compute a prop that stores the datetime in UTC+0 format + timezone picked.

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?