JediHelpers.DateUtils (jedi_helpers v0.3.1)

Copy Markdown

Converts common spreadsheet and form date values into Date structs.

Invalid values return nil, making the helper convenient for optional import fields where callers prefer a nullable result over an exception.

Summary

Functions

Parses a variety of date formats into a Date struct.

Functions

to_date(date)

@spec to_date(term()) :: Date.t() | nil

Parses a variety of date formats into a Date struct.

Supported inputs

  • "" or "0" – returns nil
  • A Date struct – returns the date itself
  • Excel serial date string (e.g. "40135") – returns the corresponding Date
  • ISO-ish strings (e.g. "2023-1-5", "2023-01-05") – returns the parsed Date
  • Strings with leading/trailing whitespace are trimmed
  • Invalid or malformed strings return nil

Use cases and results

Convert an Excel serial date during a CSV or spreadsheet import:

iex> JediHelpers.DateUtils.to_date("40135")
~D[2009-11-18]

Normalize a non-zero-padded date received from a form:

iex> JediHelpers.DateUtils.to_date("2024-1-9")
~D[2024-01-09]

Existing dates pass through unchanged, while blank and invalid values become nil:

iex> JediHelpers.DateUtils.to_date(~D[2020-05-10])
~D[2020-05-10]

iex> JediHelpers.DateUtils.to_date("")
nil

iex> JediHelpers.DateUtils.to_date("not a date")
nil