tomekowal

tomekowal

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:46Z and 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.

Showing Posts 1 to 10

Qqwy

Qqwy

TypeCheck Core Team

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.

kip

kip

ex_cldr Core Team

BCP47 defines a U extension to the language identifier that is defined in CLDR and is used in the HTTP Accept-Language header. So if the user selects an appropriate language identifier, the requested time zone can be transmitted. As an example en-AU-u-tz-ausyd-ms-metric-fw-mon means “english as spoken in Australia with time zone for Sydney, measurement system metric and the first day of the week is Monday”.

Whilst totally supported in the standard its fair to say that its not an extension often used and probably unreasonable to ask a browser user to do it this way. Totally reasonable to ask an API client to do it though.

konstantine

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.

tomekowal

tomekowal OP

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.

kip

kip

ex_cldr Core Team

I’d just like to note that if you’re dealing with timezones then you may be dealing with different cultures and languages. Which also means that the users expectation of date/time format may also be different..

jsmestad

jsmestad

Have you put this sort of functionality into a plug? This sounds incredibly useful!

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Unless the browser sends the user’s timezone in a header or something, the backend has no way of knowing what the user’s timezone is.

jsmestad

jsmestad

I am no expert, but this appeared to generate my timezone when I tried calling it in browser console.

Intl.DateTimeFormat().resolvedOptions().timeZone # returned “America/Denver”

I am no expert on the API here, but it appeared to work when I tried it.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Right, that’s the browser. But the issue is, plug doesn’t run code in the browser, plug runs code on the server, so unless the browser sends that information to the server in the HTTP request, plug can’t do anything about it.

jsmestad

jsmestad

Yeah in @konstantine’s reply he built an endpoint to send that data on DOMContentLoaded to the backend.

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)

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews