Sayfa.DateFormat (Sayfa v0.5.0)

Copy Markdown View Source

Locale-aware date formatting.

Provides centralized date formatting that replaces %B and %b format directives with translated month names from YAML translation files.

The format string is resolved in this order:

  1. Per-language date_format override in site config
  2. date_format key from YAML translations for the language
  3. Default "%b %-d, %Y" (English style)

Examples

iex> Sayfa.DateFormat.format(~D[2024-02-15], :en)
"Feb 15, 2024"

Summary

Functions

Formats a date using locale-aware month names.

Functions

format(date, lang, config \\ %{})

@spec format(
  Date.t() | DateTime.t() | NaiveDateTime.t() | String.t() | nil,
  atom(),
  map()
) ::
  String.t()

Formats a date using locale-aware month names.

Returns "" when date is nil.

Accepts %Date{}, %DateTime{}, %NaiveDateTime{}, or an ISO 8601 string. DateTime and NaiveDateTime values are converted to their date component. String values are parsed via Date.from_iso8601/1; if unparseable, the original string is returned as-is.

Examples

iex> Sayfa.DateFormat.format(~D[2024-01-15], :en)
"Jan 15, 2024"

iex> Sayfa.DateFormat.format(nil, :en)
""

iex> Sayfa.DateFormat.format(~U[2024-01-15 10:00:00Z], :en)
"Jan 15, 2024"

iex> Sayfa.DateFormat.format(~N[2024-01-15 10:00:00], :en)
"Jan 15, 2024"

iex> Sayfa.DateFormat.format("2024-01-15", :en)
"Jan 15, 2024"