diff --git a/lib/chiya_web/controllers/note_html/index.html.heex b/lib/chiya_web/controllers/note_html/index.html.heex index bbacc87..4fd190c 100644 --- a/lib/chiya_web/controllers/note_html/index.html.heex +++ b/lib/chiya_web/controllers/note_html/index.html.heex @@ -9,7 +9,7 @@ <.table id="notes" rows={@notes} row_click={&JS.navigate(~p"/admin/notes/#{&1}")}> <:col :let={note} label="Name"><%= note.name %> - <:col :let={note} label="Published at"><%= from_now_naive(note.published_at) %> + <:col :let={note} label="Published at"><%= from_now(note.published_at) %> <:col :let={note} label="Kind"><%= note.kind %> <:action :let={note}>
diff --git a/lib/chiya_web/format.ex b/lib/chiya_web/format.ex index 0e181a9..c686e66 100644 --- a/lib/chiya_web/format.ex +++ b/lib/chiya_web/format.ex @@ -1,11 +1,11 @@ defmodule ChiyaWeb.Format do - def from_now(later) do + def from_now(%DateTime{} = later) do now = DateTime.utc_now() diff = DateTime.diff(now, later) do_from_now(diff) end - def from_now_naive(later) do + def from_now(%NaiveDateTime{} = later) do now = NaiveDateTime.utc_now() diff = NaiveDateTime.diff(now, later) do_from_now(diff) @@ -13,15 +13,19 @@ defmodule ChiyaWeb.Format do def do_from_now(diff) do cond do - diff <= -24 * 3600 -> "in #{div(-diff, 24 * 3600)}d" - diff <= -3600 -> "in #{div(-diff, 3600)}h" - diff <= -60 -> "in #{div(-diff, 60)}m" - diff <= -5 -> "in #{-diff}s" + diff <= -24 * 3600 -> "in #{div(-diff, 24 * 3600)} days" + diff <= -3600 -> "in #{div(-diff, 3600)} hours" + diff <= -60 -> "in #{div(-diff, 60)} minutes" + diff <= -5 -> "in #{-diff} seconds" diff <= 5 -> "now" - diff <= 60 -> "#{diff}s ago" - diff <= 3600 -> "#{div(diff, 60)}m ago" - diff <= 24 * 3600 -> "#{div(diff, 3600)}h ago" - true -> "#{div(diff, 24 * 3600)}d ago" + diff <= 60 -> "#{diff} seconds ago" + diff <= 3600 -> "#{div(diff, 60)} minutes ago" + diff <= 24 * 3600 -> "#{div(diff, 3600)} hours ago" + true -> "#{div(diff, 24 * 3600)} days ago" end end + + def pretty_date(%NaiveDateTime{} = date) do + Calendar.strftime(date, "%d.%m.%Y") + end end