Skip to main content

formatters()

formatters cleans a value while the user types, or prints a stored value for people or for the API.

formatters(value, name, options)
ParameterDescription
valueThe value to format: text, a number, or a Date
nameA base formatter, a preset, or null to apply only the options
optionsOptional. The options that change the formatting

Returns​

formatters always returns a string. It returns "" when it can't read the value as a date or number.

Throws​

formatters throws an error for an unknown formatter or option name, an allowChars set that uses \d, \w, or \s, or an unknown timezone.

Examples​

The following examples show common calls:

formatters("air export", "titleCase")                       // "Air Export"
formatters("abcde 1234f", "pan") // "ABCDE1234F"
formatters("12.34567", "decimal", { decimalPlaces: 3 }) // "12.345"
formatters("2026-10-07 09:00:00", "displayDateTime") // "07-10-2026 14:30" (UTC shown in IST)
formatters("07-10-2026", "apiDate", { inputFormat: "DD-MM-YYYY" }) // "2026-10-07"

Base formatters​

A base formatter is a single kind of transformation, such as changing case or printing a date. The package has 14 base formatters:

FormatterWhat it does
trimRemoves spaces at both ends and collapses repeated spaces, but keeps line breaks, so it's safe on a text area
titleCaseCapitalizes each word and converts the rest to lower case
upperCaseConverts the text to upper case
lowerCaseConverts the text to lower case
numbersOnlyKeeps the digits 0 to 9 and removes everything else
integersOnlyKeeps digits and removes leading zeros
decimalKeeps digits and one decimal point, turns .5 into 0.5, and cuts decimals to decimalPlaces
datePrints a date in the form that format sets, DD-MM-YYYY by default
displayDatePrints a date as 07-10-2026
displayDateTimePrints a date and time as 07-10-2026 14:30, in timezone
apiDatePrints a date as 2026-10-07, in UTC, which is what the API stores
apiDateTimePrints a date and time as 2026-10-07 09:00:00, in UTC
currencyPrints money in the locale and currency you set, such as ₹1,25,000.00 for 125000
decimalGroupedPrints a number with thousands grouping for the locale, such as 12,50,000

Use trim when a field loses focus or before you save. If you run it on every keystroke, it removes the space that the user is typing.

Presets​

A formatter preset is a base formatter with options already set. Most formatter presets share a name with a validator preset. A field can clean itself with formatters(v, "pan") while the user types, and then be checked with validators(v, "pan"). For details, see Format and validate a form field.

The package has 17 formatter presets:

PresetDefined asExample
decimalUpTo1 to decimalUpTo6decimal, with one to six decimal placesdecimalUpTo2 turns 12.3456 into 12.34
amountdecimal, with two decimal places1,250.505 becomes 1250.50
weightdecimal, with three decimal places1500.2509 becomes 1500.250
percentagedecimal, with two decimal places and at most six characters99.999 becomes 99.99
mobileDigits, at most 1098765-43210-9 becomes 9876543210
pincodeDigits, at most six560 001 becomes 560001
emailtrim, and then lowerCaseOps@Example.COM becomes ops@example.com
panUpper case, letters and digits, at most 10abcde 1234f becomes ABCDE1234F
iecUpper case, letters and digits, at most 10abcd 12345 f becomes ABCD12345F
gstinUpper case, letters and digits, at most 1527abcde1234f1z5 becomes 27ABCDE1234F1Z5
ifscUpper case, letters and digits, at most 11hdfc 0001234 becomes HDFC0001234
vehicleNumberUpper case, letters and digits, at most 10ka-01 ab 1234 becomes KA01AB1234

Your options override a preset's options, for example formatters(v, "pan", { maxLength: 12 }).

Options​

Each option changes one step of the formatting:

OptionApplies toMeaning
allowCharsAllKeeps only these characters, written as a character set such as "0-9+()/, -"; don't use \d, \w, or \s
decimalPlacesdecimalThe number of digits kept after the decimal point
decimalPlacescurrency, decimalGroupedThe number of decimals shown
allowNegativedecimalKeeps one leading -, as in -18.5
maxLengthAllCuts the result to this many characters
localecurrency, decimalGroupedThe locale, such as "en-IN" (default)
currencycurrencyThe currency, such as "INR" (default)
formatDatesThe output pattern, built from the tokens YYYY, YY, MM, MMM, DD, HH, hh, mm, ss, and A
timezoneDatesThe zone to print in: Asia/Kolkata (default), UTC, or an offset such as "+05:30"
inputFormatDatesThe pattern of the text being read, such as "DD-MM-YYYY"
inputTimezoneDatesThe zone the text was typed in; the default is Asia/Kolkata

The steps run in this order:

  1. allowChars
  2. The formatter
  3. decimalPlaces
  4. maxLength

The following examples use options:

formatters("+91 (80) 1234-5678#", null, { allowChars: "0-9+()/, -" })      // "+91 (80) 1234-5678"
formatters("-18.567", "decimal", { allowNegative: true, decimalPlaces: 1 }) // "-18.5"
formatters("2026-10-07 09:00:00", "date", { format: "DD MMM YYYY, hh:mm A" })

Locale data​

currency and decimalGrouped use each platform's own locale data: the browser or Node in JavaScript, and the Babel library in Python. Their output can differ slightly between platforms.