166 words, 1 min read

Elixir’s Date module provides a lot of utilities for working with dates, but it doesn’t include a built-in function to get the ISO week number. Fortunately, we can rely on Erlang’s standard library to do the job.

Suppose you have a date parsed from an ISO 8601 string:

{:ok, date} = Date.from_iso8601(progress["date"])

If you want to get the ISO week number, you can convert this Date to an Erlang tuple and use :calendar.iso_week_number/1:

{:ok, date} = Date.from_iso8601(progress["date"])
{iso_year, iso_week} = :calendar.iso_week_number(Date.to_erl(date))
IO.puts("ISO week #{iso_week} of #{iso_year}")

Here’s how it works:

  • Date.to_erl/1 converts the Date struct (e.g. ~D[2025-10-16]) into an Erlang tuple {year, month, day}.
  • :calendar.iso_week_number/1 then returns a tuple {iso_year, iso_week} according to the ISO 8601 standard.

Example in the shell:

iex> {:ok, date} = Date.from_iso8601("2025-10-16")
iex> :calendar.iso_week_number(Date.to_erl(date))
{2025, 42}

If you only need the week number, you can extract it easily:

{_, week} = :calendar.iso_week_number(Date.to_erl(date))

That’s all you need — a simple, reliable way to get the ISO week number from any date in Elixir.