Skip to content

Regional formatting

Use locale for the UI language and formatLocale when dates and numbers should follow a different regional convention. A field can override the provider’s formatting locale; if neither sets one, formatting follows the UI locale.

import { CratisComponentsProvider } from '@cratis/components';
import { DatePickerInput, NumberInput } from '@cratis/components/Common';
import { useFormatting } from '@cratis/components/Formatting';
const SampleFields = () => {
const formatting = useFormatting();
return (
<>
<DatePickerInput aria-label="Sample date" value={new Date(2030, 0, 9)} onChange={() => {}} readOnly />
<NumberInput aria-label="Sample amount" value={1234.5} onChange={() => {}} readOnly />
<output>{formatting.formatNumber(1234.5, { minimumFractionDigits: 2 })}</output>
</>
);
};
export const SampleRegionalFormatting = () => (
<CratisComponentsProvider value={{ locale: 'en-US', formatLocale: 'nb-NO' }}>
<SampleFields />
</CratisComponentsProvider>
);

Here the date field shows day before month and the number uses a grouping space and decimal comma. The calendar popover and accessible control messages stay in the UI language. A language change does not change the viewer’s time zone.

Choose the formatter based on what the value represents:

Helper Use for Time zone
formatCalendarDate({ year, month, day }, locale, options?) A calendar date such as a due date. Pass Gregorian parts with a one-based month and day. Always anchors the date in UTC so the input day is not shifted. Do not pass a timeZone option.
formatInstant(dateOrOffsetISOString, locale, options?) A point in time such as the moment a report was published. Pass a Date or an ISO timestamp ending in Z or a numeric offset. Defaults to the viewer’s time zone. Pass options.timeZone to display a different zone.
formatNumber(value, locale, options?) A finite number. Use Intl.NumberFormatOptions to control grouping or precision. Not applicable.
getMonthNames(locale, width?) The twelve Gregorian month names in calendar order. Width is long (default), short, or narrow. Not applicable.
import { formatCalendarDate, formatInstant } from '@cratis/components/Formatting';
const dueDate = formatCalendarDate({ year: 2030, month: 1, day: 9 }, 'nb-NO');
const publishedAt = formatInstant('2030-01-09T01:30:00Z', 'nb-NO', { timeZone: 'America/Los_Angeles' });

The instant displays on January 8 in Los Angeles; the due date remains January 9. Date-only and offsetless strings are not accepted by formatInstant. The three string formatters return '' for absent or invalid values instead of rolling invalid dates into another month. An invalid month-name locale returns an empty array.

The functions work without Arc or a mounted provider. In React, useFormatting() returns locale and the same four helpers with that locale bound. It updates when the active formatting locale changes.