tomekowal
Displaying UTC DateTime in users browser TimeZone
Hi! I want to display lots of DateTime struct in users local timezone. Is there a standard way of doing that?
I see two options:
- getting users timezone in plug and then rendering dates server-side (but I am not sure how to get the timezone from browser to assigns)
- output the time in UTC
2020-01-18 01:03:46Zand then do the magic in JS
Is there a preferred way of doing it?
I know there were a couple of similar questions already, but they were usually about saving the date-time in the DB.
Marked As Solved
tomekowal
Thanks!
That was very valuable information! For now, I hacked JQuery+moment solution solution:
let convert_dates = function() {
$(".utc_to_local").text(function(index, utc_date) {
return moment.utc(utc_date).local().format('YYYY-DD-MM HH:mm:ss');
});
};
# for live views
$(document).on("phx:update", convert_dates);
# for regular views
$(document).ready(convert_dates);
But I think I’ll change it later. When I have signing in, I’d like to get timezone from browser via login form and then save it in session. This way, I can format dates server side which won’t require two additional JS libs.
Also Liked
Qqwy
Unfortunately the first approach will not work, since browsers do not usually send information about their computer’s configured timezone to the server.
This pretty much leaves the second approach as the only possibility.
I believe that there exist many drop-in libraries that allow you to display datetimestamps in a particular timezone in pure browser-friendly JS.
I’ve used moment.js before myself in the past, but it is by no means the only option out there.
belaustegui
I also prefer this approach of rendering UTC in the server and making the transformation to the user’s timezone in the client.
You may also want to take a look at github/time-elements, which can do this automatically for you using WebComponents.
konstantine
My approach relies on the Timex library and the ECMAScript Internationalization API:
When a request comes in, I check get_session(conn, “tmzn”). If tmzn is not yet part of my session, I add data-tmzn=“1” to my document head. JS then checks document.head.dataset.tmzn on DOMContentLoaded and, if it returns a value, it sends an async GET request to:
“/timezone?iana=” + encodeURIComponent(Intl.DateTimeFormat().resolvedOptions().timeZone)
Once the server receives this, the timezone can be added to the session, following its verification by calling Enum.member?(Timex.timezones(), iana). From that point onwards, all dates can be rendered server-side with the help of Timex.
This approach may or may not fit your use case, the most obvious issue being that it does not work (at least not out of the box) for the initial page shown to the user.
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









