Elixir Cheatsheet

Dates and Times

Use this Elixir reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.

The Four Calendar Types

TypeHoldsSigil
Dateyear-month-day~D[2026-01-15]
Timehour:minute:second(.µs)~T[13:30:00]
NaiveDateTimedate + time, no time zone~N[2026-01-15 13:30:00]
DateTimedate + time + time zone~U[2026-01-15 13:30:00Z]

Use DateTime in UTC for timestamps; NaiveDateTime only when the zone is genuinely unknown or irrelevant.

Now and Today

Date.utc_today()                    # ~D[2026-07-05]
Time.utc_now()
DateTime.utc_now()                  # ~U[2026-07-05 18:04:12.345678Z]
NaiveDateTime.utc_now()
System.os_time(:millisecond)        # integer Unix time
System.monotonic_time(:millisecond) # for measuring durations, never wall clock

Building and Parsing

Date.new(2026, 1, 15)                       # {:ok, ~D[2026-01-15]}
Date.new!(2026, 1, 15)
DateTime.new!(~D[2026-01-15], ~T[13:30:00], "Etc/UTC")

Date.from_iso8601("2026-01-15")             # {:ok, ~D[2026-01-15]}
DateTime.from_iso8601("2026-01-15T13:30:00Z")  # {:ok, dt, utc_offset}
NaiveDateTime.from_iso8601!("2026-01-15 13:30:00")

Date.to_iso8601(date)                       # "2026-01-15"
DateTime.to_iso8601(dt)                     # "2026-01-15T13:30:00Z"
to_string(date)                             # ISO 8601 via String.Chars

DateTime.from_unix!(1_768_500_000)          # Unix seconds -> DateTime
DateTime.to_unix(dt)

The standard library speaks ISO 8601 only. For "Jan 15, 2026"-style parsing/formatting, use Calendar.strftime/3 (formatting, built in) or a library like timex.

Calendar.strftime(dt, "%Y-%m-%d %H:%M")     # "2026-01-15 13:30"
Calendar.strftime(dt, "%A, %B %d")          # "Thursday, January 15"

Arithmetic and Comparison

Date.add(~D[2026-01-15], 30)                # ~D[2026-02-14]
Date.diff(~D[2026-02-14], ~D[2026-01-15])   # 30 (days)
DateTime.add(dt, 3600, :second)             # also :minute, :hour, :day
DateTime.diff(dt2, dt1)                     # seconds; DateTime.diff(dt2, dt1, :millisecond)

Date.compare(d1, d2)                        # :lt | :eq | :gt
DateTime.compare(dt1, dt2)
DateTime.before?(dt1, dt2); DateTime.after?(dt1, dt2)

Never compare DateTime structs with </> — structural comparison is wrong for dates; always use compare/2 (this also applies to Enum.sort/2: Enum.sort(dates, Date) passes the module as the comparator).

Date Components and Ranges

date.year; date.month; date.day
Date.day_of_week(~D[2026-07-05])            # 7 (Monday = 1)
Date.beginning_of_month(date); Date.end_of_month(date)
Date.beginning_of_week(date)
Date.days_in_month(date)
Date.leap_year?(date)

Date.range(~D[2026-01-01], ~D[2026-01-31])  # enumerable range of dates
|> Enum.filter(&(Date.day_of_week(&1) == 1))

Time Zones

# mix.exs: {:tzdata, "~> 1.1"}
# config: config :elixir, :time_zone_database, Tzdata.TimeZoneDatabase

DateTime.now!("America/Los_Angeles")
DateTime.shift_zone!(utc_dt, "Europe/Berlin")

Only Etc/UTC works out of the box; any other zone needs a time zone database (usually tzdata). Standard practice: store and compute in UTC, shift to the user's zone at the display edge.

Measuring Elapsed Time

t0 = System.monotonic_time(:millisecond)
work()
elapsed_ms = System.monotonic_time(:millisecond) - t0

{micros, result} = :timer.tc(fn -> work() end)