Reputation: 4341
I'd like to alter the format of a Timex DateTime field to a single text input. When I let Phoenix take care of the formatting, it looks like this:
2017-04-06 23:03:44.513454+10:00 AEST Australia/Hobart
Note that I'm using use Timex.Ecto.Timestamps, usec: true
- so it includes the microseconds.
Here's what I would like it to look like:
2017-04-06 23:03:44 +10:00
Is there a way to control the format of the date via the form text_input
tag? Or is there another way to do this?
Here's the schema I'm using:
schema "log" do
field :start_date, Timex.Ecto.DateTime
field :end_date, Timex.Ecto.DateTime
field :comment, :string
timestamps
end
My form looks something like this:
<%= form_for @changeset, @action, fn f -> %>
<%= if @changeset.action do %>
<div class="alert alert-danger">
<p>Oops, something went wrong! Please check the errors below.</p>
</div>
<% end %>
<div class="form-group">
<%= label f, :start_date, class: "control-label" %>
<%= text_input f, :start_date, class: "form-control" %>
<%= error_tag f, :start_date %>
</div>
<div class="form-group">
<%= label f, :end_date, class: "control-label" %>
<%= text_input f, :end_date, class: "form-control" %>
<%= error_tag f, :end_date %>
</div>
<div class="form-group">
<%= label f, :comment, class: "control-label" %>
<%= textarea f, :comment, class: "form-control" %>
<%= error_tag f, :comment %>
</div>
<div class="form-group">
<%= submit "Submit", class: "btn btn-primary" %>
</div>
<% end %>
Upvotes: 1
Views: 2280
Reputation: 222168
You can get the input's value using Phoenix.HTML.Form.input_value/2
and you can specify the value to show to the user by passing value: ...
to text_input
. Combine this with removing the microseconds from the value and you'll get the time in the same format without any microseconds:
text_input f, :start_date, class: "form-control", value: %{input_value(f, :start_date) | microsecond: {0, 0}}
Upvotes: 2