glintl

Format dates and times in many languages, on Erlang and JavaScript.

import glintl
import glintl/locale/en_gb
import gleam/time/duration
import gleam/time/timestamp

let formatter =
  glintl.date_time_formatter(en_gb.locale(), date: glintl.Long, time: glintl.Short)

glintl.format(formatter, timestamp.system_time(), offset: duration.hours(1))
// -> "25 October 2026 at 23:40"

On Erlang the formats come from data baked into this package, made from ICU when the package is built. On JavaScript the runtime’s own Intl.DateTimeFormat is used, so the output can differ slightly between targets and between JavaScript runtimes.

Types

The calendars glintl can format dates in.

Each locale has a default: most use Gregorian, Thai uses Buddhist, Persian uses Persian, and Saudi Arabic uses IslamicUmmAlQura.

On Erlang, calendars with their own months work for years 1 to 4000 of the Gregorian calendar, and dates outside that are written in the Gregorian calendar instead. These are every calendar but Gregorian, Iso8601, Buddhist, Japanese and RepublicOfChina.

pub type Calendar {
  Gregorian
  Iso8601
  Buddhist
  Japanese
  RepublicOfChina
  Persian
  Indian
  Hebrew
  Chinese
  Dangi
  Coptic
  Ethiopic
  EthiopicAmeteAlem
  IslamicCivil
  IslamicTabular
  IslamicUmmAlQura
}

Constructors

  • Gregorian
  • Iso8601

    The Gregorian calendar, with ISO 8601 rules for weeks and patterns.

  • Buddhist

    The Thai solar calendar: Gregorian, with years counted from 543 BC.

  • Japanese

    Gregorian, with years counted in Japanese eras.

  • RepublicOfChina

    The Minguo calendar used in Taiwan: Gregorian, with years counted from 1912.

  • Persian

    The Solar Hijri calendar used in Iran and Afghanistan.

  • Indian

    The Indian national calendar.

  • Hebrew
  • Chinese
  • Dangi

    The Korean calendar.

  • Coptic
  • Ethiopic

    The Ethiopian calendar, with years counted in the Amete Mihret era.

  • EthiopicAmeteAlem

    The Ethiopian calendar, with years counted in the Amete Alem era.

  • IslamicCivil

    The tabular Islamic calendar, counting from 16 July 622 in the Julian calendar, as civil tables do.

  • IslamicTabular

    The tabular Islamic calendar, counting from 15 July 622 in the Julian calendar, the astronomical start.

  • IslamicUmmAlQura

    The Umm al-Qura calendar used in Saudi Arabia.

Formats moments in time for one locale and style.

Make one and reuse it: making a formatter does the lookup work once, so formatting is cheap.

pub opaque type Formatter

A language and region to format for, such as British English.

Get one from a locale module, like glintl/locale/en_gb, or by tag from glintl/locales.

pub opaque type Locale

How much detail to show, from most to least.

These are the same as the dateStyle and timeStyle options of JavaScript’s Intl.DateTimeFormat. In British English:

StyleDateTime
FullSunday, 25 October 202623:40:58 GMT+01:00
Long25 October 202623:40:58 GMT+1
Medium25 Oct 202623:40:58
Short25/10/202623:40
pub type Style {
  Full
  Long
  Medium
  Short
}

Constructors

  • Full
  • Long
  • Medium
  • Short

Values

pub fn date_formatter(
  locale: Locale,
  style style: Style,
) -> Formatter

Format only the date.

pub fn date_time_formatter(
  locale: Locale,
  date date: Style,
  time time: Style,
) -> Formatter

Format both the date and the time.

pub fn format(
  formatter: Formatter,
  timestamp: timestamp.Timestamp,
  offset offset: duration.Duration,
) -> String

Format a moment in time, as seen from a place offset ahead of UTC.

The offset is rounded down to whole minutes. Time zones are written as a GMT offset, like GMT+1, as glintl knows the offset but not the zone’s name.

pub fn locale_calendar(locale: Locale) -> Calendar

The calendar a locale formats dates in.

pub fn locale_tag(locale: Locale) -> String

The BCP 47 tag of a locale, such as "en-GB".

pub fn time_formatter(
  locale: Locale,
  style style: Style,
) -> Formatter

Format only the time.

pub fn with_calendar(
  locale: Locale,
  calendar: Calendar,
) -> Locale

Format dates in another calendar.

en_gb.locale()
|> glintl.with_calendar(glintl.Hebrew)
|> glintl.date_formatter(style: glintl.Long)
|> glintl.format(timestamp.system_time(), offset: duration.hours(1))
// -> "14 Heshvan 5787"
✨ Search Document