How to show and edit/bind input=date on a live view page?

In a model I have a field

  filed :my_dt, :naive_datetime

As far as I’ve found out, in Phoenix there’s no Html helper for a date alone which will generate a calendar picker. Therefore, I’m using an html tag for it, and it’s on a LiveView page:

  <input type="date" name="my_model[my_dt]" value={@my_dt_without_time} />

That is, the time part should always remain 00:00 – may be ignored, for now. I don’t need a time zone either, the UTC should be used.

How will I properly bind a my_dt to the date tag such that
a) it’ll show itself properly, whenever my_dt is present in the DB; and
b) it’ll get edited and updated properly too

?

I’m aware that it’ll require pre-formatting it on backend and post-processing, and this will be ok.

There’s a date_input helper in Phoenix.HTML.Form.

1 Like

Yes, now I see.

However, it’ll won’t be able to simply bind it to the tag if a value contain both data and time? I don’t need the time part for now, but my the data type is naive_datetime nonetheless.

For instance, 2022-11-18T00:00:00 won’t get displayed in it correctly – it’l lremain uninitialized as if a value was nil.

Will it require that I cut off the time part before displaying it on a page? And then attach 00:00 back priour to updating a model? Or is there a more standard way to deal with this?

Date.from_datetime as the binding.

Translate back in the change handler if casting into a datetime :slight_smile: