Localization provider

Lab component

Also known as

Provider

The LocalizationProvider component is essential for configuring the locale and date adapter in components that utilize date objects.

Note: This tab list includes overflow; tab positions may be inaccurate or change when a tab is selected
  • Wrap your application with typically one LocalizationProvider to define the date adapter for date based controls.
  • LocalizationProvider can be nested with different adapters or locales.
  • When you use DatePicker, DateInput or Calendar components.
  • When you want to configure your own date adapter.
  • When you want to change the locale to non-US.
  • When you want to use a legacy data adapter, such as Moment as part of a migration journey to a maintained adapter.
  • Inside an application that has no date controls.

To import LocalizationProvider from the core Salt package, use:

To import your chosen adapter adapter use one of the following :

Each adapter imports the date library defined by your peer dependency but for dayjs and luxon, you may need to provide a preconfigured instance.

Passing dayjs to AdapterDayJs enables you to preconfigure defaults.

Passing moment-timezone to AdapterMoment adapter enables timezone support.

NameTypeDescriptionDefault

The SaltDateAdapter interface is implemented by all adapters, it provides a standardized way to interact with various date libraries, offering methods for date manipulation, formatting, and comparison. It normalizes their APIs so we can use the underlying date API within components.

  • TDate: Represents the type of the date object used by the adapter.
  • TLocale: Represents the type of the locale, defaulting to any.
PropertyTypeDescription
localeTLocale (optional)The locale used by the date adapter for formatting and parsing.
libstringThe name of the date library being used.

Creates a date object from a string or returns an invalid date.

ParameterTypeDescription
valuestring | undefinedThe date string to parse.
timezonestring (optional)The timezone to use.
localeTLocale (optional)The locale to use for parsing.

Returns: TDate - The parsed date object or an invalid date object.

Formats a date object using the specified format string.

ParameterTypeDescription
dateTDateThe date object to format.
formatRecommendedFormats (optional)The format string to use.
localeTLocale (optional)The locale to use for formatting.

Returns: string - The formatted date string.

Compares two date objects.

ParameterTypeDescription
dateATDateThe first date object.
dateBTDateThe second date object.

Returns: number - 0 if equal, 1 if dateA is after dateB, -1 if dateA is before dateB.

Parses a date string using the specified format.

ParameterTypeDescription
valuestringThe date string to parse.
formatstringThe format string to use.
localeTLocale (optional)The locale to use for parsing.

Returns: ParserResult<TDate> - An object containing the parsed date and any errors.

Checks if a date object is valid.

ParameterTypeDescription
dateanyThe date object to check.

Returns: boolean - true if the date is valid, false otherwise.

Adds time to a date object.

ParameterTypeDescription
dateTDateThe date object to add to.
durationObjectAn object specifying the time to add (days, weeks, months, etc.).

Returns: TDate - The resulting date object.

Subtracts time from a date object.

ParameterTypeDescription
dateTDateThe date object to subtract from.
durationObjectAn object specifying the time to subtract (days, weeks, months, etc.).

Returns: TDate - The resulting date object.

Sets specific components of a date object.

ParameterTypeDescription
dateTDateThe date object to modify.
componentsObjectAn object specifying the components to set (day, month, year, etc.).

Returns: TDate - The resulting date object.

Checks if two date objects are the same based on the specified granularity.

ParameterTypeDescription
dateATDateThe first date object.
dateBTDateThe second date object.
granularity"day" | "month" | "year"The granularity to compare by.

Returns: boolean - true if the dates are the same, false otherwise.

Gets the start of a specified time period for a date object.

ParameterTypeDescription
dateTDateThe date object.
granularity"day" | "week" | "month" | "year"The time period.
localeTLocale (optional)The locale to use.

Returns: TDate - The date object representing the start of the period.

Gets the end of a specified time period for a date object.

ParameterTypeDescription
dateTDateThe date object.
granularity"day" | "week" | "month" | "year"The time period.
localeTLocale (optional)The locale to use.

Returns: TDate - The date object representing the end of the period.

Gets the current date with the time set to the start of the day.

ParameterTypeDescription
localeTLocale (optional)The locale to use.

Returns: TDate - The current date at the start of the day.

Gets the current date and time.

ParameterTypeDescription
localeTLocale (optional)The locale to use.

Returns: TDate - The current date and time.

Gets the day of the week for a date object.

ParameterTypeDescription
dateTDateThe date object.
localeTLocale (optional)The locale to use.

Returns: number - The day of the week as a number (0-6).

Gets the day of the month for a date object.

ParameterTypeDescription
dateTDateThe date object.

Returns: number - The day of the month as a number (1-31).

Gets the month for a date object.

ParameterTypeDescription
dateTDateThe date object.

Returns: number - The month as a number (0-11).

Gets the year for a date object.

ParameterTypeDescription
dateTDateThe date object.

Returns: number - The year as a number.

Gets the time components for a date object.

ParameterTypeDescription
dateTDateThe date object.

Returns: TimeFields - An object containing the hour, minute, second, and millisecond.

Gets the name of the day of the week.

ParameterTypeDescription
downumberThe day of the week as a number (0-6).
format"long" | "short" | "narrow"The format for the day name.
localeany (optional)The locale to use.

Returns: string - The name of the day of the week.

Clones the date object.

ParameterTypeDescription
dateTDateThe date object to clone.

Returns: TDate - The cloned date object.

The following table lists the supported format types for date and time formatting.

Format TypeDescription
YYYYFull year (e.g., 2023)
YYTwo-digit year (e.g., 23)
MMMMFull month name (e.g., July)
MMMAbbreviated month name (e.g., Jul)
MMTwo-digit month (e.g., 07)
MMonth (e.g., 7)
DDTwo-digit day of month (e.g., 09)
DDay of month (e.g., 9)
ddddFull day name (e.g., Monday)
dddAbbreviated day name (e.g., Mon)
ddTwo-letter day name (e.g., Mo)
dDay of week number (1-7)
HHTwo-digit hour (24-hour)
HHour (24-hour)
hhTwo-digit hour (12-hour)
hHour (12-hour)
mmTwo-digit minute
mMinute
ssTwo-digit second
sSecond
AAM/PM
aam/pm
ZTimezone offset
ZZTimezone offset

For example to parse a date you need specify a format string

To format a date object you need a format string

Not normally necessary, but in order to add a new adapter that is a valid TDate, you will need to augment DateFrameworkTypeMap.