GoulvenClech

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 :slight_smile:

Marked As Solved

kokolegorille

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 :slight_smile:

17
Post #9

Also Liked

kip

kip

ex_cldr Core Team

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

stefanchrobot

I think the cleanest way is to have a setting in the UI where the user can configure it.

kip

kip

ex_cldr Core Team

12 different timezones for France and its overseas territories! And a huge longitudinal span as well!

Where Next?

Popular in Questions Top

gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call t...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New

Other popular topics Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 43657 311
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36432 110
New
Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 127536 1222
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

We're in Beta

About us Mission Statement