Localize.DateTime.Parser (Localize v1.4.0)

Copy Markdown View Source

Parses a locale-formatted string as whichever temporal value it turns out to be — a date, a time, a datetime, or a date range.

Localize.Date.parse/2, Localize.Time.parse/2, Localize.DateTime.parse/2 and Localize.Interval.parse/2 each parse one shape and expect the caller to know which. parse/2 here is for the case where the shape is not known up front: a single text field that may carry any of "2026-05-16", "14:30", "May 16, 2026 2:30 PM" or "May 5 – May 10, 2026". The caller pattern-matches on the returned struct to discover what was parsed.

Summary

Functions

Parses a locale-formatted string as a date, time, datetime, or date range — whichever matches.

Functions

parse(input, options \\ [])

@spec parse(String.t(), Keyword.t()) ::
  {:ok,
   Date.t()
   | Time.t()
   | NaiveDateTime.t()
   | DateTime.t()
   | Date.Range.t()
   | map()
   | {map(), map()}}
  | {:error, Exception.t()}

Parses a locale-formatted string as a date, time, datetime, or date range — whichever matches.

Arguments

  • input is the string to parse.

  • options is a keyword list of options.

Options

Returns

Examples

iex> Localize.DateTime.Parser.parse("March 22, 2026", locale: :en)
{:ok, ~D[2026-03-22]}

iex> Localize.DateTime.Parser.parse("3:45 PM", locale: :en)
{:ok, ~T[15:45:00]}