LoginSignup
5
0

More than 5 years have passed since last update.

日時タプルを文字列に変換

Last updated at Posted at 2018-01-25

Ecto等で取得した日時は、{ { 2018, 1, 26 }, { 0, 17, 53 } }のような、タプルとなっているため、Web上でそのまま表示できないので、文字列に変換してあげる必要がある

Elixirらしく、値の型を、関数のガード節で呼び分けする

defmodule On do
    def view( dt_tuple ) when is_tupl( dt_tuple ) do
        dt_tuple 
        |> Timex.to_datetime 
        |> DateTime.to_string 
        |> String.slice( 0..-2 )
    end
    def view( value), do: value
end

Phoenix等で実際に使う際は、こんな感じ

<table>
<tr>
<%= for caption <- map_list |> List.first |> Map.keys() do %>
    <th><%= caption %></th>
<% end %>
</tr>
<tr>
<%= for record <- map_list do %>
    <%= for column <- record |> Map.keys() do %>
    <td><%= record[ column ] |> On.view %></td>
    <% end %>
<% end %>
</tr>
</table>

p.s.福岡でElixirプロダクション採用を推進するコミュニティ「fukuoka.ex」のMeetUp#5「福岡におけるElixirの進化」は、こんな感じでワイワイやっています(地味に女子率多し)

image.png

5
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
0