Unix Timestamp in Elixir
Elixir uses DateTime and System.system_time/1 for Unix timestamps. Both return integers in the time unit you specify (:second, :millisecond, :microsecond) — making unit conversion explicit and clear.
Code Examples
Current timestamp (seconds)
System.system_time(:second)
Returns the current Unix timestamp in seconds as an integer. The :second unit is the most common for REST APIs and database storage.
Current timestamp (milliseconds)
System.system_time(:millisecond)
Returns milliseconds since epoch. Pass :microsecond for even finer precision. These are native Erlang/OTP time units.
Using DateTime module
DateTime.utc_now() |> DateTime.to_unix(:second)
Pipeline-style: get the current UTC DateTime, then convert to Unix seconds. Prefer this in Phoenix/Ecto code where you're already working with DateTime structs.
Convert timestamp back to DateTime
DateTime.from_unix!(1708560000, :second)
Converts a Unix timestamp to a DateTime struct in UTC. The bang (!) variant raises if the timestamp is out of range; use DateTime.from_unix/2 for the {:ok, dt} tuple form.
Ecto — inserting a timestamp
# In a migration or changeset: NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)
Ecto typically stores timestamps as NaiveDateTime (UTC, no timezone). truncate/1 removes sub-second precision, which most databases don't store by default.
Note
Elixir inherits Erlang's time handling — System.system_time/1 is the idiomatic choice for Unix timestamps. In Phoenix/Ecto applications, DateTime.utc_now() is preferred because it returns a DateTime struct you can pipe directly into Ecto changesets and schema fields.
Frequently Asked Questions
How do I get the current Unix timestamp in Elixir?
Use System.system_time(:second), which returns the current Unix timestamp as an integer — the number of seconds since January 1, 1970 UTC. It's part of Elixir's standard library with no additional dependencies. Use :millisecond for milliseconds.
What is the difference between System.system_time/1 and DateTime.utc_now() in Elixir?
System.system_time(:second) returns an integer (e.g., 1708560000), which is ideal for APIs, storage, and comparisons. DateTime.utc_now() returns a DateTime struct with timezone information, which is better for Phoenix/Ecto code where you need to pipe into changesets, format dates, or do calendar arithmetic.
How do I convert a Unix timestamp to a DateTime in Elixir?
Use DateTime.from_unix!(timestamp, :second), which returns a UTC DateTime struct. The bang (!) variant raises ArgumentError if the timestamp is out of range. Use the non-bang DateTime.from_unix/2 for the {:ok, datetime} tuple form, which is safer in production code.
Why does Elixir use atoms like :second and :millisecond for time units?
Elixir inherits this from Erlang's OTP time system. Using atoms makes the unit explicit in the function call, preventing the common bug of accidentally mixing seconds and milliseconds. The supported units are :second, :millisecond, :microsecond, and :nanosecond.
How do I store a Unix timestamp in an Ecto schema?
For Ecto, use NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second) for the inserted_at/updated_at timestamps. For custom Unix integer fields, declare them as :integer in your schema and insert System.system_time(:second) directly.
Main Epoch Converter
Use the homepage when you need epoch to date, date to epoch, or a full epoch converter
This Elixir guide shows how to create and parse Unix timestamps in code. If your next step is broader search intent like epoch converter, epoch to date, epoch time to date, or unix epoch converter, jump back to the live homepage tools to paste a raw Unix value, auto-detect seconds versus milliseconds, and copy the readable answer instantly.