👋 Next.js Conf 2024: Come build, party, run, and connect with us! See all events

Handling timezones in Sanity.io richDate fields

9 replies
Last updated: Oct 20, 2021
Hello Sanity Community. I have users enter data in a richDate field. The issue is that users in different timezones enter data so the times are all off for users. Is it possible to specify a timezone in which the data is entered? This timezone also has PDT and PST…
Oct 20, 2021, 12:54 PM
Which type are you using to save the dates in the schema? The dateTime -type says it saves the date in UTC.
Oct 20, 2021, 12:56 PM
The type in the schema is the richDate type. I guess my issue is not with sanity itself then. I guess to guide me in the right direction, how are others handling daylight savings with users entering the same time at different timezones…
Oct 20, 2021, 1:03 PM
I would probably convert all dateTimes to the same timezone on saving in order to have some common ground for comparison. Then I’d allow the users browsers to convert the dates back into their timezone. Potentially with information about which timezone it uses, so users dont assume the time is given for another timezone.

Dealing with time and timezones is a horrendous minefield of gotchas, so I would heavily recommend finding a battle-tested package and using it for all it is worth rather than attempting to handle everything yourself. 🙂
Oct 20, 2021, 1:07 PM
That would be exactly what I need. Does sanity allow for a datetime or richDate type field to be saved as a specific timezone?
Oct 20, 2021, 1:08 PM
Users are using the studio to enter date data.
Oct 20, 2021, 1:09 PM
My understanding is that there doesnt exist a
richDate
field by default in Sanity. If you read the documentation I linked in the first post you’ll see that the
dateTime
type automatically converts times to UTC when saving. 🙂
Oct 20, 2021, 1:09 PM
oh. I did install the sanity richDate plugin to get the timezone data for the entered user, hoping it would help me find a solution. Ok, I finally understand. Thanks for your help.
Oct 20, 2021, 1:18 PM
The
richDate
plugin does not seem maintained anymore. 🙂 The last official release was when it was still a member of Sanitys monorepo. It has been moved out of that into its own repo , but there have not been any releases associated with that repo since then.
The code in your plugin should probably still be the same tho. We can see that in the
schema used internally by the `richDate` plugin , the field
utc
uses
dateTime
.
Oct 20, 2021, 1:28 PM
Yeah, I just noticed that. I installed since there was a “Pro Tip” in the datetime type in the sanity docs.
https://www.sanity.io/docs/datetime-type
Oct 20, 2021, 1:29 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?