Date Format in form.html.eex

I currently have a field in my form.
Type Date.
Everything working perfectly …

The only issue is …
It is entering this format yyyy-mm-dd

I needed to handle this for the dd/mm/yyyy format

What would be the way to do this?
I did not want to use javascript because in the elixir, all this answers me …
Calendar and everything else …

form.html.eex

<label>Expiração</label>
<%= text_input f, :expira, class: "form-control span2 col-md-9", type: "date" %>
<%= error_tag f, :expira %>

The thing is, when you use an input HTML field with the attribute type=date, browsers will configure this field differently based on:

  • what browser it is (FireFox shows it differently from Chrome which shows it differently from Internet Explorer). Mobile browsers also do their own thing.
  • What locale the user is in: In locales where the dd-mm-yyyy format is used, that one might be shown. In locales where mm-dd-yyyy is used, that one might be shown.
  • Some browsers still do not support it, and then it will degrade to a normal text field.

Information on the Mozilla Developers Network. Note that the value that is used behind the schenes (for both setting and retrieving its value) will always be in the ISO 8601 format (that is the yyyy-mm-dd one.)

If you want to do it differently, you will probably need a JavaScript ‘date picker’ (there are many variants out there).

Oh, and if it is at all possible to educate the users of your application to use the yyyy-mm-dd format, please do; that would be the absolute best, future-proof, solution :wink: .

1 Like

Considering that is the ISO8601 DateTime standard, anyone that is not using it will break somehow and somewhere (the heck is mm-dd-yyyy, that is weirdly mixed… o.O).

That is what they use in the United States, Micronesia and some other countries.

I have been using long time ago jqueryui datepicker.

I would not recommand jquery now, but it is possible to define date format on the ui side, then it would be converted to ISO8601.

In my country, it’s dd/mm/yyyy

Not here, and I work in the central USA. ^.^
ISO8601 all the way!

1 Like