GoulvenClech
Display DateTime in user's timezone
Hi everyone!
I work for a French company and, with our marine territories, our customers live in many timezones. So I’m wondering what is, in 2022, the correct way to display dates in browser timezone with Phoenix?
I saw on a topic of 2020 this solution:
<time id="updated_at" datetime="2020-06-30 12:21:17Z"></time>
<span id="updated_at_view" phx-update="ignore"></span>
<script>
const view_element = document.getElementById('updated_at_view');
document.addEventListener('phx:update', () => {
const source_element = document.getElementById('updated_at');
view_element.innerText = new Date(source_element.getAttribute('datetime')).toLocaleString();
})
</script>
Is this really the best solution? Shouldn’t I use a hook? Isn’t there an elixir solution?
Thanks for your help ![]()
Marked As Solved
kokolegorille
If You use liveview, You can set timezone, timezone_offset in the live socket, this way You can retrieve this information server side, and do the translation in the server.
let liveSocket = new LiveSocket("/live", Socket, {
hooks: Hooks,
params: {
_csrf_token: csrfToken,
locale: Intl.NumberFormat().resolvedOptions().locale,
// timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
// USE POLYFILL FOR IE11
timezone: moment.tz.guess(),
timezone_offset: -(new Date().getTimezoneOffset() / 60),
}
});
But as other have also mentionned, I prefer to do it client side ![]()
Also Liked
kip
I too think that whatever timezone you think the user wants dates/times displayed in, give them an option to tell you what they want. Assuming browser timezone, or timezone based on IP address or locale or some other automated mechanism can create a worse experience. Let say I’m living in Singapore but travelling in the US and trying to schedule a meeting for when I get back. I don’t want dates/times to be rendered based upon where I am, but based upon where I’m going.
stefanchrobot
kip
12 different timezones for France and its overseas territories! And a huge longitudinal span as well!
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex









