Unlock seamless workflows and faster delivery with our latest releases - Join the deep dive

Discussion about handling time zones in Sanity.io and potential solutions using momentjs and custom fields.

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.
May 6, 2022, 3:37 PM
Iā€™m using a simple field with a list of options and deal with time zone on the frontend.
May 6, 2022, 4:09 PM
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?
May 6, 2022, 4:12 PM
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.
May 6, 2022, 4:15 PM
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 })}
May 6, 2022, 4:37 PM
Itā€™s working fine for simple use cases
May 6, 2022, 4:37 PM
As the datetime field is computed using the browser's locale time zone, isn't this causing problem?
May 6, 2022, 4:43 PM
For my usage it was fine, but I only needed to display hours on a footer, so not that important šŸ˜…
May 6, 2022, 4:59 PM
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.
May 6, 2022, 6:25 PM
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.
May 6, 2022, 6:26 PM
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.
May 6, 2022, 7:20 PM

Sanityā€“ build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?