tomr

tomr

Get current date/month/year for date_select in template

Hey everyone,

I am struggeling with getting a default value (today for the client) into a date_select. Based on the Phoenix Doc there is a function called Calendar, but it appears to be not available in templates:

        <div class="input-group">
          <%= date_select f, :date, builder: fn b -> %>
            <%= b.(:day, [default: :calendar.day(), class: "form-select"]) %>
            <%= b.(:month, [default: :calendar.month(), class: "form-select"]) %>
            <%= b.(:year, [default: :calendar.year(), class: "form-select"]) %>
          <% end %>
        </div>

gives me:

function :calendar.day/0 is undefined or private

same for

Phoenix.Calendar.day()
Calendar.day()

What is it that I am missing?

Without the builder the following worked:

<%= date_select f, :date, [default: :calendar.local_time(), class: "form-select"] %>

Help is much appreciated!

Marked As Solved

kip

kip

ex_cldr Core Team

As seen by the client isn’t possible to be derived by the server alone and it needs to be communicated by the client to the server. Therefore no elixir standard function can answer that question for you.

There are various mechanisms to do that:

  1. Use the locale as defined in the accept language header, if that header includes the U extension tz. No browser seems to do that, but its possible for an API-based application.

  2. Reverse lookup the IP address to establish a location and then use a library like tz_world to determine the time zone. Not great since IP addresses are not a reliable indicator of the users preference

  3. Ask the user what timezone they would prefer and send that as a parameter to the request. In my opinion this is to be preferred - users should have the ability to express their preferences. Of course this preference could be stored in a user profile making it easy to determine for authorised users.

Also Liked

kip

kip

ex_cldr Core Team

The following functions will help (note the caveats below):

  • Date.utc_today/0. See h Date.utc_today in iex
  • :calendar.local_time/0 returns {date, time}

Caveats

When you say “today”, which today do you mean? Today, as seen by the server’s time or by the local time where the server is located? By the time as seen by the client? The concept of “what is the date today” has nuances that affect the user experience.

Where Next?

Popular in Questions Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
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

We're in Beta

About us Mission Statement