Temporal.PlainMonthDay.prototype.toLocaleString()
Limited availability
This feature is not Baseline because it does not work in some of the most widely-used browsers.
Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.
The toLocaleString()
method of Temporal.PlainMonthDay
instances returns a string with a language-sensitive representation of this month-day. In implementations with Intl.DateTimeFormat
API support, this method delegates to Intl.DateTimeFormat
.
Every time toLocaleString
is called, it has to perform a search in a big database of localization strings, which is potentially inefficient. When the method is called many times with the same arguments, it is better to create a Intl.DateTimeFormat
object and use its format()
method, because a DateTimeFormat
object remembers the arguments passed to it and may decide to cache a slice of the database, so future format
calls can search for localization strings within a more constrained context.
Syntax
toLocaleString()
toLocaleString(locales)
toLocaleString(locales, options)
Parameters
The locales
and options
parameters customize the behavior of the function and let applications specify the language whose formatting conventions should be used.
In implementations that support the Intl.DateTimeFormat
API, these parameters correspond exactly to the Intl.DateTimeFormat()
constructor's parameters. Implementations without Intl.DateTimeFormat
support return the exact same string as toString()
, ignoring both parameters.
locales
Optional-
A string with a BCP 47 language tag, or an array of such strings. Corresponds to the
locales
parameter of theIntl.DateTimeFormat()
constructor. options
Optional-
An object adjusting the output format. Corresponds to the
options
parameter of theIntl.DateTimeFormat()
constructor. Thecalendar
option must be provided with the same value as this month-day's calendar. Regarding the date-time component options and the style shortcuts (dateStyle
andtimeStyle
), the options should follow one of these forms:- Provide none of them:
month
andday
will default to"numeric"
. - Provide
dateStyle
only: it expands tomonth
andday
formats. - Provide some date-time component options, where at least one of them is
month
orday
. Only the specified date components will be included in the output.
- Provide none of them:
See the Intl.DateTimeFormat()
constructor for details on these parameters and how to use them.
Return value
A string representing the given month-day according to language-specific conventions.
In implementations with Intl.DateTimeFormat
, this is equivalent to new Intl.DateTimeFormat(locales, options).format(monthDay)
, where options
has been normalized as described above.
Note:
Most of the time, the formatting returned by toLocaleString()
is consistent. However, the output may vary between implementations, even within the same locale — output variations are by design and allowed by the specification. It may also not be what you expect. For example, the string may use non-breaking spaces or be surrounded by bidirectional control characters. You should not compare the results of toLocaleString()
to hardcoded constants.
Exceptions
RangeError
-
Thrown if any of the options is invalid.
TypeError
-
Thrown if any of the options is not of the expected type.
Examples
Using toLocaleString()
Basic use of this method without specifying a locale
returns a formatted string in the default locale and with default options.
// Note that just specifying "08-01" defaults to the ISO 8601 calendar,
// which throws an error if the locale's default calendar is not ISO 8601.
const md = Temporal.PlainMonthDay.from("2021-08-01[u-ca=gregory]");
console.log(md.toLocaleString()); // 8/1 (assuming en-US locale and Gregorian calendar)
If the month-day's calendar doesn't match the locale's default calendar, even when its calendar is iso8601
, an explicit calendar
option must be provided with the same value.
const md = Temporal.PlainMonthDay.from("08-01");
md.toLocaleString("en-US", { calendar: "iso8601" }); // 08-01
Using toLocaleString() with options
You can customize which parts of the month-day are included in the output by providing the options
parameter.
const md = Temporal.PlainMonthDay.from("2021-08-01[u-ca=gregory]");
md.toLocaleString("en-US", { dateStyle: "full" }); // August 1
md.toLocaleString("en-US", { month: "long" }); // August
md.toLocaleString("en-US", { day: "numeric" }); // 1
Specifications
Specification |
---|
Temporal proposal # sec-temporal.plainmonthday.prototype.tolocalestring |
Browser compatibility
BCD tables only load in the browser