AdonisJS API
    Preparing search index...

    Core formatter to format different values using the I18n API. Provides methods for formatting numbers, currencies, dates, times, relative time, plurals, lists, and display names.

    const formatter = new Formatter('en')
    const formatted = formatter.formatNumber(1234.56)
    console.log(formatted) // "1,234.56"

    Hierarchy (View Summary)

    Index
    • Switches the formatter to use a different locale

      Parameters

      • locale: string

        The new locale to use for formatting

      Returns void

      formatter.switchLocale('fr')
      console.log(formatter.locale) // 'fr'
    • Format a numeric value for a given style

      Parameters

      • value: string | number | bigint

        The numeric value to format (string, number, or bigint)

      • Optionaloptions: NumberFormatOptions

        Optional formatting options (style, currency, etc.)

      Returns string

      Formatted number string according to the current locale

      formatter.formatNumber(1234.56) // "1,234.56" (for en locale)
      formatter.formatNumber(1234.56, { style: 'percent' }) // "123,456%"
    • Format a numeric value to a currency

      Parameters

      • value: string | number | bigint

        The numeric value to format as currency

      • options: CurrencyFormatOptions

        Currency formatting options including currency code

      Returns string

      Formatted currency string

      formatter.formatCurrency(1234.56, { currency: 'USD' }) // "$1,234.56"
      formatter.formatCurrency(1000, { currency: 'EUR' }) // "€1,000.00"
    • Format value as a date. The method accepts the following data types:

      • Date instance
      • Luxon DateTime instance
      • Number representing a unix timestamp
      • String representing an ISO date/time value

      Parameters

      • value: string | number | Date | DateTime<boolean>

        The date value to format

      • Optionaloptions: DateTimeFormatOptions

        Optional date formatting options

      Returns string

      Formatted date string according to the current locale

      formatter.formatDate(new Date()) // "12/25/2023" (for en-US locale)
      formatter.formatDate('2023-12-25T10:00:00Z', { dateStyle: 'full' })
      // "Monday, December 25, 2023"
    • Format value as time. The method accepts the following data types:

      • Date instance
      • Luxon DateTime instance
      • Number representing a unix timestamp
      • String representing an ISO date/time value

      Parameters

      • value: string | number | Date | DateTime<boolean>

        The time value to format

      • Optionaloptions: TimeFormatOptions

        Optional time formatting options

      Returns string

      Formatted time string according to the current locale

      formatter.formatTime(new Date()) // "10:30:00 AM" (for en locale)
      formatter.formatTime('2023-12-25T14:30:00Z', { timeStyle: 'short' }) // "2:30 PM"
    • Format value as relative diff between the current time and the given value. The following data types are allowed:

      • Date instance
      • Luxon DateTime instance
      • Number representing the diff value in provided units. If the "unit" is auto, the number will be considered as a diff in milliseconds
      • String representing an ISO date/time value

      Parameters

      • value: string | number | Date | DateTime<boolean>

        The date/time value to format relatively

      • unit: "auto" | RelativeTimeFormatUnit

        The unit for relative formatting or 'auto' for automatic unit selection

      • Optionaloptions: RelativeTimeFormatOptions

        Optional relative time formatting options

      Returns string

      Formatted relative time string

      formatter.formatRelativeTime(-1, 'day') // "1 day ago"
      formatter.formatRelativeTime(new Date(Date.now() + 3600000), 'auto') // "in 1 hour"
      formatter.formatRelativeTime('2023-12-25T10:00:00Z', 'auto') // "2 days ago"
    • Format the value to its plural counterpart using locale-specific plural rules

      Parameters

      • value: string | number

        The numeric value to determine plural form for

      • Optionaloptions: PluralRulesOptions

        Optional plural rules options

      Returns string

      Plural category string ("zero", "one", "two", "few", "many", or "other")

      formatter.formatPlural(1) // "one"
      formatter.formatPlural(2) // "other" (for English)
      formatter.formatPlural(0) // "other" (for English)
    • Format an array of strings to a sentence using locale-appropriate conjunctions

      Parameters

      • list: Iterable<string>

        Iterable of strings to format into a list

      • Optionaloptions: ListFormatOptions

        Optional list formatting options (type, style)

      Returns string

      Formatted list string

      formatter.formatList(['apple', 'banana', 'orange']) // "apple, banana, and orange"
      formatter.formatList(['red', 'blue'], { type: 'disjunction' }) // "red or blue"
    • Format region, currency, language codes to their display names

      Parameters

      • code: string

        The code to get display name for (language, region, currency, etc.)

      • options: DisplayNamesOptions

        Display names options specifying the type of code

      Returns string | undefined

      Localized display name for the code

      formatter.formatDisplayNames('US', { type: 'region' }) // "United States"
      formatter.formatDisplayNames('USD', { type: 'currency' }) // "US Dollar"
      formatter.formatDisplayNames('en', { type: 'language' }) // "English"