Discussion about handling time zones in Sanity.io and potential solutions using momentjs and custom fields.
10 replies
Last updated: May 6, 2022
S
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
š§I've seen the
richDatetype 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
T
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
T
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
S
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
T
I use something like this:
And I handle the rest on the frontend
{ 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', ... ]
{date.toLocaleTimeString('en-US', { timeZone })}
May 6, 2022, 4:37 PM
T
Itās working fine for simple use cases
May 6, 2022, 4:37 PM
S
As the datetime field is computed using the browser's locale time zone, isn't this causing problem?
May 6, 2022, 4:43 PM
T
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
J
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.
Iād love to hear how others have solved this.
May 6, 2022, 6:25 PM
J
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
S
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.