97 words, 1 min read
⚠️ This post links to an external website. ⚠️
Here’s an easy way to shift the time/date to the user’s timezone in Phoenix LiveView without introducing new controls in the UI.
# app.jslet timezoneOffset = new Date().getTimezoneOffset()const liveSocket = new LiveSocket("/live", Socket, {longPollFallbackMs: 2500,params: {_csrf_token: csrfToken, timezone_offset: timezoneOffset}})# index.exon mount {__MODULE__, :assign_timezone_offset}def assign_timezone_offset(_params, _session, socket) dotimezone =if connected?(socket) doPhoenix.LiveView.get_connect_params(socket)["timezone_offset"]else0end{:cont, assign(socket, :timezone_offset, timezone_offset)}enddef render(assigns) do~H"""Created at: #{NaiveDateTime.add(@post.created_at, timezone_offset, :second)}"""endOf course, you’ll want to extract the on_mount hook so it can be used by multiple liveviews.
continue reading on elixirdrops.net
If this post was enjoyable or useful for you, please share it! If you have comments, questions, or feedback, you can email my personal email. To get new posts, subscribe use the RSS feed.