Getting the current date and or time in Elixir

From Erlang 19.1:

:os.system_time(:millisecond)

From Erlang 18:

:os.system_time(:milli_seconds)

Gives the current UNIX timestamp UTC.

Thanks @HenrikN for linking the blog post http://michal.muskala.eu/2015/07/30/unix-timestamps-in-elixir.html


To answer your question more specifically (though I really appreciate Onorio's recommendation to use Timex!), you can get the current date and time from the Erlang standard lib using :calendar.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.


You can use DateTime.utc_now/0 to get UTC DateTime.

Tags:

Elixir