9
votes

I'm coming from a Rails background and trying to learn the Phoenix Framework. In Rails I used to do this in my footer so that the year is always up to date:

<%= Date.now.year %>

What is the Phoenix/Elixir equivalent? How can I show the current year in the view?

Thank you for your time.

1

1 Answers

27
votes

Elixir 1.3.0 (which has just been released) has a very simple date library. For example, in order to get the current year, you could do something like:

DateTime.utc_now |> Map.fetch!(:year)

@Dogbert below has also suggested a much easier way than the above:

DateTime.utc_now.year

Which returns

2016

Another solution (if you don't want to use Elixir 1.3.0) is to use an external library such as Timex

Where you could do something very similar:

Date.today |> Map.fetch!(:year)

Which will again return

2016