1
votes

In Elixir/Phoenix, how can I format a date-time field without any additional dependencies? The type of the field in Postgresql is timestampz and I want to display it as "05, Dec, 2015".

2

2 Answers

3
votes

Here's one way:

def format_datetime(%Ecto.DateTime{day: day, month: month, year: year}) do
  "#{format_day(day)}, #{format_month(month)}, #{year}"
end

defp format_day(day) when day < 10, do: "0#{day}"
defp format_day(day), do: "#{day}"

defp format_month(1), do: "Jan"
defp format_month(2), do: "Feb"
defp format_month(3), do: "Mar"
defp format_month(4), do: "Apr"
defp format_month(5), do: "May"
defp format_month(6), do: "Jun"
defp format_month(7), do: "Jul"
defp format_month(8), do: "Aug"
defp format_month(9), do: "Sep"
defp format_month(10), do: "Oct"
defp format_month(11), do: "Nov"
defp format_month(12), do: "Dec"

Test:

iex(1)> format_datetime(%Ecto.DateTime{day: 5, month: 12, year: 2015})
"05, Dec, 2015"
1
votes

You can roll your own solution pretty easily, although you will need to have a list of month names:

defmodule DateTimeFormat do
  @months %{1 => "Jan", 2 => "Feb", 3 => "Mar", 4 => "Apr",
            5 => "May", 6 => "Jun", 7 => "Jul", 8 => "Aug",
            9 => "Sep", 10 => "Oct", 11 => "Nov", 12 => "Dec"}

  def format(timestamp) do
    '~4..0B, ~ts, ~2..0B'
    |> :io_lib.format([timestamp.year, @months[timestamp.month], timestamp.day])
    |> List.to_string
  end
end

Alternatively, you could do the formatting directly in Postgres. Whether this is practical will depend on your use case, but I found it very useful for example for generating reports with a single query:

to_char(my_column, 'MM, Mon, YYYY')