Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Current Year in a View Using the Phoenix Framework

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.

like image 926
Andrew Hendrie Avatar asked Jun 21 '16 21:06

Andrew Hendrie


People also ask

How do I get current date in Elixir?

You can use DateTime.universal_time() or :calendar. local_time() . There are a number of useful functions for working with Erlang's datetime type in the calendar module, but they are somewhat limited, and don't provide you with anything for parsing or formatting dates, which is where Timex comes in.

What is Phoenix framework used for?

Phoenix is a web development framework written in the functional programming language Elixir. Phoenix uses a server-side model–view–controller (MVC) pattern. Based on the Plug library, and ultimately the Cowboy Erlang framework, it was developed to provide highly performant and scalable web applications.

Is Phoenix framework popular?

Phoenix: The Elixir FrameworkPhoenix is a popular tool written in Elixir and used with this language. On the server side, Phoenix uses a scheme close to the Model View Controller. Like Elixir, it was designed to provide applications capable of working with a very large number of users.

Is Phoenix framework fast?

Phoenix delivers better and faster performance as compared to Rails. Phoenix applications are easier to maintain and have high scalability, while Rails applications require effort and resources to scale. Phoenix is also more reliable for handling high traffic and concurrency applications.


1 Answers

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

like image 144
Harrison Lucas Avatar answered Oct 03 '22 20:10

Harrison Lucas