Include this in your schema and stop worrying about string dates!
```
field :some_date, ParsedDate
```
defmodule ParsedDate do
use Ecto.Type
def type, do: :date
def cast(str_date) when is_binary(str_date) do
case DateTimeParser.parse_date(str_date) do
{:ok, date} -> {:ok, date}
_ -> :error
end
end
def cast(%Date{} = date), do: {:ok, date}
def cast(_), do: :error
def load(%Date{} = date) do
{:ok, date}
end
def dump(%Date{} = date), do: {:ok, date}
def dump(_), do: :error
end
39
upvotes