Voile.Utils.DateHelper (Voile v0.1.26)

Copy Markdown View Source

Helper functions for date and time formatting and conversion across the application. Enhanced with Indonesian localization and country-specific formatting.

Summary

Functions

Formats datetime for display with both local time and relative time.

Parses an ISO 8601 datetime string and returns a DateTime struct.

Returns a list of supported country codes with their timezones.

Converts a UTC datetime to a human-readable relative time. Supports both English and Indonesian.

Converts a UTC datetime to local date only (no time).

Converts a UTC datetime to local time and formats it for display.

Converts local datetime input to UTC for database storage.

Functions

display_datetime(utc_datetime, opts \\ [])

Formats datetime for display with both local time and relative time.

Examples

iex> utc_time = ~U[2023-03-15 14:30:00Z]
iex> Voile.Utils.DateHelper.display_datetime(utc_time)
"15/03/2023 21:30 WIB (2 days ago)"

iex> Voile.Utils.DateHelper.display_datetime(utc_time, format: :indonesian)
"Rabu, 15 Maret 2023 21:30 (2 hari yang lalu)"

parse(datetime_string)

Parses an ISO 8601 datetime string and returns a DateTime struct.

Examples

iex> Voile.Utils.DateHelper.parse("2025-10-06T05:22:32Z")
{:ok, ~U[2025-10-06 05:22:32Z]}

supported_countries()

Returns a list of supported country codes with their timezones.

time_ago(utc_datetime, language \\ :english)

Converts a UTC datetime to a human-readable relative time. Supports both English and Indonesian.

Examples

iex> now = DateTime.utc_now()
iex> past = DateTime.add(now, -3600, :second)
iex> Voile.Utils.DateHelper.time_ago(past)
"1 hour ago"

iex> Voile.Utils.DateHelper.time_ago(past, :indonesian)
"1 jam yang lalu"

to_local_date(utc_datetime, timezone \\ "Asia/Jakarta")

Converts a UTC datetime to local date only (no time).

Examples

iex> utc_time = ~U[2023-03-15 14:30:00Z]
iex> Voile.Utils.DateHelper.to_local_date(utc_time)
"15/03/2023"

to_local_time(utc_datetime, timezone \\ "Asia/Jakarta", format \\ "%d/%m/%Y %H:%M %Z")

Converts a UTC datetime to local time and formats it for display.

Parameters

  • utc_datetime - The UTC datetime to convert (%DateTime{} or %NaiveDateTime{})
  • timezone - The target timezone (default: "Asia/Jakarta")
  • format - The format type:
    • String (strftime format) - e.g., "%d/%m/%Y %H:%M %Z"
    • :indonesian - "Senin, 6 Oktober 2025 12:22"
    • :indonesian_short - "Sen, 6 Okt 2025 12:22"
    • :indonesian_date - "Senin, 6 Oktober 2025"
    • Country code (e.g., "US", "JP") - Country-specific format

Examples

iex> utc_time = ~U[2023-03-15 14:30:00Z]
iex> Voile.Utils.DateHelper.to_local_time(utc_time)
"15/03/2023 21:30 WIB"

iex> Voile.Utils.DateHelper.to_local_time(utc_time, "Asia/Jakarta", :indonesian)
"Rabu, 15 Maret 2023 21:30"

iex> Voile.Utils.DateHelper.to_local_time(utc_time, "America/New_York", "US")
"03/15/2023 10:30:00 AM"

to_utc(datetime, timezone \\ "Asia/Jakarta")

Converts local datetime input to UTC for database storage.

Examples

iex> local_time = ~N[2023-03-15 21:30:00]
iex> Voile.Utils.DateHelper.to_utc(local_time, "Asia/Jakarta")
~U[2023-03-15 14:30:00Z]