DateTime.now in Elixir and Ecto

darko picture darko · Sep 5, 2016 · Viewed 9.4k times · Source

I want to get the current date-time stamp in Phoenix/Elixir without a third-party library. Or simply, I want something like DateTime.now(). How can I do that?

Answer

Dogbert picture Dogbert · Sep 5, 2016

Ecto has Ecto.DateTime.utc/1 to get the current time in UTC:

iex(1)> Ecto.DateTime.utc
#Ecto.DateTime<2016-09-05 13:30:04>
iex(2)> Ecto.DateTime.utc(:usec) # include microseconds
#Ecto.DateTime<2016-09-05 13:30:18.367318>

If you want the current time in the local system's timezone, you can do:

Ecto.DateTime.from_erl(:erlang.localtime)