Temporal.PlainMonthDay.from()
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 Temporal.PlainMonthDay.from() static method creates a new Temporal.PlainMonthDay object from another Temporal.PlainMonthDay object, an object with month and day properties, or an RFC 9557 string.
Syntax
Temporal.PlainMonthDay.from(info)
Temporal.PlainMonthDay.from(info, options)
Parameters
info-
One of the following:
- A
Temporal.PlainMonthDayinstance, which creates a copy of the instance. - An RFC 9557 string containing a date and optionally a calendar. If the calendar is not
iso8601, a year is required. - An object containing the following properties (in the order they are retrieved and validated):
calendarOptional-
A string that corresponds to the
calendarIdproperty. Defaults to"iso8601". All other properties are interpreted in this calendar system (unlike theTemporal.PlainMonthDay()constructor, which interprets the values in the ISO calendar system). SeeIntl.supportedValuesOf()for a list of commonly supported calendar types. day-
An integer that corresponds to the
dayproperty. Must be positive regardless of theoverflowoption. eraanderaYear-
A string and an integer that can be used instead of
year. SeeeraanderaYearofPlainDate. Are only used if the calendar system has eras.eraanderaYearmust be provided simultaneously. Ifmonthis specified, at least one oferaYear(together withera) oryearmust be provided. If all ofera,eraYear, andyearare provided, they must be consistent. month-
A positive integer that can be used instead of
monthCode. SeemonthofPlainDate. Must be positive regardless of theoverflowoption. Ifmonthis provided, and the calendar is notiso8601, thenyear(oreraYeartogether witheraas a substitution) must be provided too, because the samemonthmay map to multiple possiblemonthCodevalues in different years. At least one ofmonthormonthCodemust be provided. If bothmonthandmonthCodeare provided, they must be consistent. monthCode-
Corresponds to the
monthCodeproperty. At least one ofmonthormonthCodemust be provided. If bothmonthandmonthCodeare provided, they must be consistent. year-
An integer used to disambiguate
monthif provided, because for some calendars, the samemonthcan mean differentmonthCodein different years. SeeyearofPlainDate. If a year is provided, then theoverflowoption validates the month-day in the given year, not just any year. Ifmonthis specified, at least one oferaYear(together withera) oryearmust be provided. If all ofera,eraYear, andyearare provided, they must be consistent.
- A
optionsOptional-
An object containing the following property:
overflowOptional-
A string specifying the behavior when a date component is out of range (when using the object
info). Possible values are:"constrain"(default)-
The date component is clamped to the valid range.
"reject"-
A
RangeErroris thrown if the date component is out of range.
Return value
A new Temporal.PlainMonthDay object, representing the month and day specified by info in the specified calendar.
Each PlainMonthDay stores a whole ISO 8601 date internally, which has the same month-day in the target calendar as what's exposed. The reference year is visible when stringifying with toString(), which outputs an ISO date. The reference year is chosen arbitrarily but consistently (that is, every (monthCode, day) pair always maps to the same ISO reference year). It does not use the year provided in the input. Instead, the reference year is chosen by finding the latest date before December 31, 1972 that has the same month-day in the target calendar, or the earliest date after December 31, 1972 if no such date exists.
For example, for Gregorian-derived calendars, the reference year is 1972. For the Hebrew calendar, the reference year is 1972 in the Gregorian calendar, but if the month is Adar I (M05L), which is a leap month, the reference year is 1970 (5730 in Hebrew calendar) instead, because the next leap year is 1973 (5733 in Hebrew calendar), which is after 1972.
This reference year canonicalization ensures that equals() can directly compare the underlying ISO dates without extra computation.
Exceptions
TypeError-
Thrown in one of the following cases:
infois not an object or a string.optionsis not an object orundefined.- The provided properties are insufficient to unambiguously determine a date. You usually need to provide a
year(oreraanderaYear), amonth, and aday, or amonthCodeand aday.
RangeError-
Thrown in one of the following cases:
- The provided properties that specify the same component are inconsistent.
- The provided non-numerical properties are not valid; for example, if
monthCodeis never a valid month code in this calendar. - The provided numerical properties are out of range, and
options.overflowis set to"reject". - The info is not in the representable range, which is ±(108 + 1) days, or about ±273,972.6 years, from the Unix epoch.
Examples
>Creating a PlainMonthDay from an object
// Month code + day
const md = Temporal.PlainMonthDay.from({ monthCode: "M05", day: 2 });
console.log(md.toString()); // 05-02
// Month + day (only for ISO calendar)
const md2 = Temporal.PlainMonthDay.from({ month: 7, day: 1 });
console.log(md2.toString()); // 07-01
// Year + month + day
const md3 = Temporal.PlainMonthDay.from({ year: 2021, month: 7, day: 1 });
console.log(md3.toString()); // 07-01
// Year + month + day in a different calendar (where year is required)
const md4 = Temporal.PlainMonthDay.from({
year: 2021,
month: 7,
day: 1,
calendar: "hebrew",
});
console.log(md4.toString()); // 1972-03-16[u-ca=hebrew]
// Month code + day in a different calendar
const md5 = Temporal.PlainMonthDay.from({
monthCode: "M05L",
day: 1,
calendar: "hebrew",
});
console.log(md5.toString()); // 1970-02-07[u-ca=hebrew]
Controlling overflow behavior
By default, out-of-range values are clamped to the valid range. A month-day without an explicit reference year is valid as long as there exists one year in which it is valid, even if it doesn't appear every year. If year, month, and day are all given, then the rules for mapping to a valid month-day could be complex and specific to each calendar, but here's the usual behavior:
- If the
year/monthcombination is invalid, themonthis clamped to obtain a validmonthCodein the year. - If the
year/monthCodecombination is invalid, a different year is chosen to keep themonthCodeas-is. - The
dayis clamped in the given year-month to obtain a valid month-day.
This is slightly different from usual date clamping, which favors the year over the month code.
// Month always out of range
const md1 = Temporal.PlainMonthDay.from({ month: 13, day: 1 });
console.log(md1.toString()); // 12-01
// Month out of range for the specific year: 5732 is not a Hebrew leap year,
// so month is clamped to 12 to resolve to a valid monthCode
const md2 = Temporal.PlainMonthDay.from({
year: 5732,
month: 13,
day: 1,
calendar: "hebrew",
});
console.log(md2.toLocaleString("en-US", { calendar: "hebrew" })); // 1 Elul
const underlyingDate = Temporal.PlainDate.from(md2.toString());
console.log(underlyingDate.year, underlyingDate.month); // 5732 12
// Month code exists but not for the specific year: 5731 is not a Hebrew leap year,
// so a different year is chosen to keep the monthCode as M05L
const md3 = Temporal.PlainMonthDay.from({
year: 5731,
monthCode: "M05L",
day: 1,
calendar: "hebrew",
});
console.log(md3.toLocaleString("en-US", { calendar: "hebrew" })); // 1 Adar I
const underlyingDate2 = Temporal.PlainDate.from(md3.toString());
console.log(underlyingDate2.year, underlyingDate2.monthCode); // 5730 M05L
// Day always out of range
const md4 = Temporal.PlainMonthDay.from({ month: 2, day: 30 });
console.log(md4.toString()); // 02-29
// Day out of range for the specific year-month
const md5 = Temporal.PlainMonthDay.from({ year: 2021, month: 2, day: 29 });
console.log(md5.toString()); // 02-28
You can change this behavior to throw an error instead:
Temporal.PlainMonthDay.from(
{ year: 2021, month: 13, day: 1 },
{ overflow: "reject" },
);
// RangeError: date value "month" not in 1..12: 13
Specifications
| Specification |
|---|
| Temporal> # sec-temporal.plainmonthday.from> |
Browser compatibility
Loading…