Date Formats, Phoenix and Ecto

Hi All

I have, what I hope, is a simple question. I’ve a Phoenix web form with a date which is displayed using a JQuery calendar. The problem I have is the date is show as dd/mm/yyyy i.e 02/12/1964. When Phoenix (or more exactly) Ecto tries to parse this it expects the format to be a map of year/month/day. I thought, wrongly that cast would automatically convert from the string date “02/12/1964” to the Elixir format date but I was wrong.

I’m assuming that I have to do some form of conversion but really don’t know where this would go?

any help gratefully received.

cheers

Dave

Timex is a great library for all sorts of things date / time related, and includes a very flexible parser:

Docs on the parse functions here:

https://hexdocs.pm/timex/Timex.html#parse/2

Parse the date out before handing it in to your Ecto calls.

1 Like

thanks @aseigo. So in Phoenix I’d do something like (not formal Elixir code):

 def create(conn, "person" => person_params) do
     elixir_date = Some-Timex-Function(person_params["my-date])
    Map.put(person_params, "my-date", elixir-date)
    Call Person Create 

cheers

Dave

The nicest solution would be to create a custom ecto type, casting from the desired format.

1 Like

Thanks @michalmuskala.

Having slept on the problem overnight I’d come to the same conclusion. I suppose my biggest surprise is that this isn’t catered for in standard Phoenix/Ecto as I would have thought it would be a very common situation.

cheers

Dave

Too many ways to format dates though. Your example of dd/mm/yyyy is, to be quick, really-friggin-weird of a format. ^.^

ISO standard formatting (you know, yyyy/mm/dd-etc…) is pretty well supported in Elixir native though, considering it is a standard format. :slight_smile:

Timex works well for converting about any format you want though. :slight_smile:

Cheers @OvermindDL1 - I’ll not take offence :smile:

Oh none meant at all! I just had no clue how that could be parsed unambiguously. ^.^

The ISO standard format is unambiguous. :slight_smile:

Lol. I’ve actually been thinking about your reply and I’ve changed my input to ISO standard so all is well with the world.

cheers

Dave

That looks like quite a normal way to write down a date to me. Most hand-written (and by that I mean not produced by a computer system - can be printed) dates I came across in my life with were either dd.mm.yyyy or dd/mm/yyyy.

1 Like

Or use Phoenix datetime_select which generates select fields and have none of those issues. :slight_smile:

2 Likes

Ah but Jose my users love their pop up calendars :wink:

1 Like

Being in the central USA I see either yyyy/mm/dd or mm/dd/yyyy, dd/mm/yyyy I never see around here, ever. ^.^

I remember solving this problem in CakePHP by using its date select with jquery. You just hide the selects and hook them to changes in the calendar. The form submission never knows the difference and as a bonus you get a non-JavaScript fallback.

@brightball - love this. I’ll give it a shot.

cheers

Dave

1 Like

DD/MM/YYYY is the most used format worldwide and MM/DD/YYYY the least.

7 Likes

This is what I do. I have an <input> element of type date or whatever I want it to be, with an is attribute. If no javascript or any issues with javascript it falls back to the ugly but functional native (and often broken, but eh) browser date picker, with javascript then it loads my polymer date element of the full material’y style. The is attribute is awesome and I really really am hating those idiots who are trying to remove it from the standardization process (they don’t care about fallback to older browsers or anything of the sort).

1 Like

@shortlyportly, can u post how u solved it using Timex.parse ?
which timex function did the job ?

Some-Timex-Function(person_params["my-date]

This is how one would use Timex to parse this date format:

Timex.parse!("22/02/2017", "{D}/{M}/{YYYY}") |> Timex.to_date
2 Likes

I don’t think there is any other country that use this strange format, sorry but in this case Americans are the weirdos :stuck_out_tongue_winking_eye:

1 Like