You can use iso8601 datetime strings between your javascript and elixir backend.
The simplest way to convert this to an Ecto schema is to declare the field as utc_datetime
You can then use Ecto.Changeset.cast
to automatically convert the string to a %DateTime{}
struct.
defmodule Blog do
use Ecto.Schema
schema "blog" do
field :publish_at, :utc_datetime
end
def new(params) do
%Blog{}
|> Ecto.Changeset.cast(params, [:publish_at])
|> Ecto.Changeset.apply_changes()
end
end
iex> Blog.new(%{"publish_at" => "2017-01-01T12:23:34Z"})
%Blog{__meta__: #Ecto.Schema.Metadata<:built, "blog">, id: nil, publish_at: %DateTime{calendar: Calendar.ISO, day: 1, hour: 12, microsecond: {0, 0}, minute: 23, month: 1, second: 34, std_offset: 0, time_zone: "Etc/UTC", utc_offset: 0, year: 2017, zone_abbr: "UTC"}}
Note: Any timezone offset will be discarded using this approach.