Showing relative time in HEEX using Timex

almirsarajcic

almirsarajcic

Created 6 months ago

Here’s how to show when a record was created relative to the current time, e.g. “3 minutes ago”.

Add the Timex dependency ({:timex, "~> 3.7"}) to your mix.exs file, then use it:

def created_at(assigns) do
  ~H"""
  <time datetime={Timex.format!(@created_at, "{ISO:Extended:Z}")}>
    <%= Timex.format!(@created_at, "{relative}", :relative) %>
  </time>
  """
end

and the result will look like this:

<time datetime="2024-12-18T11:01:00Z">4 minutes ago</time>