View Source Bonfire.Common.DatesTimes (Bonfire v0.9.10-classic-beta.156)

Date/time helpers

Summary

Functions

Returns a list of available format keys for the given locale.

Returns a keyword list of available date/time formats for the given locale.

Takes a ULID ID (or an object with one) or a DateTime struct, and turns the date into a relative phrase, e.g. 2 days ago.

Takes an object (or string with an ULID) and converts the ULID ID to a DateTime struct.

Formats a DateTime struct or date into a string using Cldr.DateTime.to_string/2.

Formats a Date struct or date into a string using Cldr.Date.to_string/2.

Checks if a DateTime is in the future relative to the current time.

Generates a ULID based on a DateTime or a string representation of a date/time, but only if the date/time is in the past.

Returns the current UTC DateTime.

Returns a DateTime in the past, relative to the current time, by subtracting a specified amount of time.

Checks if a DateTime is in the past relative to the current time.

Converts a DateTime struct to a relative date string. Uses Cldr.DateTime or Timex libraries.

Removes a specified amount of time from a DateTime.

Converts various formats into a DateTime struct.

Converts various formats into a DateTime struct.

Functions

Link to this function

available_format_keys(scope \\ DateTime, locale \\ Cldr.get_locale())

View Source

Returns a list of available format keys for the given locale.

Examples

> available_format_keys()
[:short, :medium, :long, :full]  # Example output
Link to this function

available_formats(scope \\ DateTime, locale \\ Cldr.get_locale())

View Source

Returns a keyword list of available date/time formats for the given locale.

Examples

> available_formats()
[short: "Short", medium: "Medium", long: "Long", full: "Full"]  # Example output
Link to this function

date_from_now(ulid_or_date, opts \\ [])

View Source

Takes a ULID ID (or an object with one) or a DateTime struct, and turns the date into a relative phrase, e.g. 2 days ago.

Examples

> date_from_now(%{id: "01FJ6G6V9E7Y3A6HZ5F2M3K4RY"})
"25 days ago"  # Example output

> date_from_now("01FJ6G6V9E7Y3A6HZ5F2M3K4RY")
"25 days ago"  # Example output
Link to this function

date_from_pointer(object)

View Source

Takes an object (or string with an ULID) and converts the ULID ID to a DateTime struct.

Examples

> date_from_pointer("01FJ6G6V9E7Y3A6HZ5F2M3K4RY")
%DateTime{year: 2024, month: 7, day: 25, ...}  # Example output
Link to this function

format(date, opts \\ [])

View Source

Formats a DateTime struct or date into a string using Cldr.DateTime.to_string/2.

Examples

> format(DateTime.now!("Etc/UTC"))
"Jul 25, 2024, 11:08:21 AM"
Link to this function

format_date(date, opts \\ [])

View Source

Formats a Date struct or date into a string using Cldr.Date.to_string/2.

Examples

> format(DateTime.now!("Etc/UTC"))
"Jul 25, 2024, 11:08:21 AM"

Checks if a DateTime is in the future relative to the current time.

Examples

iex> future?(%Date{year: 3020, month: 7, day: 25})
true  # Example output

iex> future?(%Date{year: 2023, month: 7, day: 25})
false  # Example output
Link to this function

maybe_generate_ulid(date_time_or_string)

View Source

Generates a ULID based on a DateTime or a string representation of a date/time, but only if the date/time is in the past.

Examples

> maybe_generate_ulid(%Date{year: 2024, month: 7, day: 25})
"01J3KJZZ00X1EXD6TZYD3PPDR6"  # Example output

> maybe_generate_ulid("2024-07-25")
"01J3KJZZ00X1EXD6TZYD3PPDR6"  # Example output

Returns the current UTC DateTime.

Examples

> now()
%DateTime{year: 2024, month: 7, day: 25, ...}  # Example output
Link to this function

past(amount_to_remove, unit \\ :second)

View Source

Returns a DateTime in the past, relative to the current time, by subtracting a specified amount of time.

Examples

> past(10, :day)
%DateTime{year: 2024, month: 7, day: 15, ...}  # Example output

Checks if a DateTime is in the past relative to the current time.

Examples

iex> past?(%Date{year: 3020, month: 7, day: 25})
false  # Example output

iex> past?(%Date{year: 2023, month: 7, day: 24})
true   # Example output
Link to this function

relative_date(date_time, opts \\ [])

View Source

Converts a DateTime struct to a relative date string. Uses Cldr.DateTime or Timex libraries.

Examples

iex> relative_date(DateTime.now!("Etc/UTC"))
"now"  # Example output
Link to this function

remove(dt, amount_to_remove, unit \\ :second)

View Source

Removes a specified amount of time from a DateTime.

Examples

> remove(%Date{year: 2024, month: 7, day: 25}, 10, :day)
%DateTime{year: 2024, month: 7, day: 15, ...}  # Example output

Converts various formats into a DateTime struct.

Examples

iex> to_date(%Date{year: 2024, month: 7, day: 25})
%Date{year: 2024, month: 7, day: 25} 

iex> to_date("2024-07-25")
%Date{year: 2024, month: 7, day: 25}

iex> to_date(1656115200000)
%Date{year: 2022, month: 6, day: 25}  

iex> to_date(%{"day" => 25, "month" => 7, "year" => 2024})
%Date{year: 2024, month: 7, day: 25}  

Converts various formats into a DateTime struct.

Examples

> to_date_time(%Date{year: 2024, month: 7, day: 25})
%DateTime{year: 2024, month: 7, day: 25, ...}  # Example output

> to_date_time("2024-07-25")
%DateTime{year: 2024, month: 7, day: 25, ...}  # Example output

> to_date_time(1656115200000)
%DateTime{year: 2024, month: 7, day: 25, ...}  # Example output

> to_date_time(%{"day" => 25, "month" => 7, "year" => 2024})
%DateTime{year: 2024, month: 7, day: 25, ...}  # Example output