Skip to content

Would love a guide on handling timezones in SvelteKit #13696

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
johnnysprinkles opened this issue Apr 9, 2025 · 3 comments
Open

Would love a guide on handling timezones in SvelteKit #13696

johnnysprinkles opened this issue Apr 9, 2025 · 3 comments
Labels
documentation Improvements or additions to documentation

Comments

@johnnysprinkles
Copy link
Contributor

Describe the problem

Dealing with timezones in a Universal SvelteKit app isn't that obvious. The user's timezone is only known clientside so I stumbled my way through a solution that:

a) First visit, the page server-side renders with a default timezone of say Los Angeles
b) Then a cookie gets set with the minutes of offset, in a cookie called 'TZ'
c) Subsequent visits confirm that the TZ there matches the current one (it'll change if you move across the world or if daylight savings time toggles)

This gives you a "flash of incorrect timezone" on the first visit, which is unavoidable, but fixed as soon as the page hydrates.

I think a reference solution for this would involve loaders in +layout.server.js to read the cookie, +layout.js to call new Date(), and some code somewhere to set the cookie if not set yet, and also possibly code to put it in context to make available to all components in the tree.

Bonus points if it's reactive and the minutes of offset update when Daylight Savings Time becomes active or inactive.

Any interest?

Describe the proposed solution

See above.

Alternatives considered

No response

Importance

nice to have

Additional Information

No response

@eltigerchino eltigerchino added the documentation Improvements or additions to documentation label Apr 10, 2025
@gterras
Copy link
Contributor

gterras commented Apr 13, 2025

"Automatic TZ discovery" sounds like an overly specific issue VS a manually set user setting (which should be offered anyway to cover all user cases, especially if time has such a critical context). The daylight savings part is just a per-request implementation detail from say a cookie or a param, I can' really see what this would add to the docs.

@johnnysprinkles
Copy link
Contributor Author

Yeah it's actually not all the complicated, it just seems so because I'm now getting back into the swing of using SvelteKit. Go ahead and close but I might make a simple HOWTO on this at some point, for external to the docs like a blog post.

@gterras
Copy link
Contributor

gterras commented Apr 13, 2025

Note that I'm not a maintainer just a user, but this is most likely a discussion rather than an issue.

If you write something of interest you can submit it to sveltesociety "recipies".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

3 participants