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.
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With