This feature is not Baseline because it does not work in some of the most widely-used browsers.
The Temporal.PlainYearMonth.from() static method creates a new Temporal.PlainYearMonth object from another Temporal.PlainYearMonth object, an object with year and month properties, or an RFC 9557 string.
Temporal.PlainYearMonth.from(info) Temporal.PlainYearMonth.from(info, options)
infoOne of the following:
Temporal.PlainYearMonth instance, which creates a copy of the instance.iso8601, a day is required.calendar OptionalA string that corresponds to the calendarId property. See Intl.supportedValuesOf() for a list of commonly supported calendar types. Defaults to "iso8601". All other properties are interpreted in this calendar system (unlike the Temporal.PlainYearMonth() constructor, which interprets the values in the ISO calendar system).
era and eraYearA string and an integer that correspond to the era and eraYear properties. Are only used if the calendar system has eras. era and eraYear must be provided simultaneously. If they are not provided, then year must be provided. If all of era, eraYear, and year are provided, they must be consistent.
monthCorresponds to the month property. Must be positive regardless of the overflow option.
monthCodeCorresponds to the monthCode property. If it is not provided, then month must be provided. If both month and monthCode are provided, they must be consistent.
yearCorresponds to the year property.
options OptionalAn object containing the following property:
overflow OptionalA 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 RangeError is thrown if the date component is out of range.
A new Temporal.PlainYearMonth object, representing the year and month specified by info in the specified calendar.
Each PlainYearMonth stores a whole ISO 8601 date internally, which has the same year-month in the target calendar as what's exposed. The reference day is visible when stringifying with toString(), which outputs an ISO date. The reference day is chosen arbitrarily but consistently; that is, every (year, month) pair always maps to the same ISO reference day. It does not use the day provided in the input. Instead, the reference day is always chosen to be the first valid day of the month.
This reference day canonicalization ensures that equals() can directly compare the underlying ISO dates without extra computation.
TypeErrorThrown in one of the following cases:
info is not an object or a string.options is not an object or undefined.year (or era and eraYear) and a month (or a monthCode).RangeErrorThrown in one of the following cases:
monthCode is never a valid month code in this calendar.options.overflow is set to "reject".// Year + month code
const ym = Temporal.PlainYearMonth.from({ year: 2021, monthCode: "M05" });
console.log(ym.toString()); // 2021-05
// Year + month
const ym2 = Temporal.PlainYearMonth.from({ year: 2021, month: 7 });
console.log(ym2.toString()); // 2021-07
// Year + month in a different calendar
const ym3 = Temporal.PlainYearMonth.from({
year: 5730,
month: 6,
calendar: "hebrew",
});
console.log(ym3.toString()); // 1970-02-07[u-ca=hebrew]
// Year + month code in a different calendar
const ym4 = Temporal.PlainYearMonth.from({
year: 5730,
monthCode: "M05L",
calendar: "hebrew",
});
console.log(ym4.toString()); // 1970-02-07[u-ca=hebrew]
By default, out-of-range values are clamped to the valid range.
const ym1 = Temporal.PlainYearMonth.from({ year: 2021, month: 13 });
console.log(ym1.toString()); // 2021-12
// 5732 is not a Hebrew leap year, so a different monthCode is chosen
const ym2 = Temporal.PlainYearMonth.from({
year: 5732,
monthCode: "M05L",
calendar: "hebrew",
});
console.log(ym2.toLocaleString("en-US", { calendar: "hebrew" })); // Adar 5732
const underlyingDate = Temporal.PlainDate.from(ym2.toString());
console.log(underlyingDate.year, underlyingDate.monthCode); // 5732 M06
You can change this behavior to throw an error instead:
Temporal.PlainYearMonth.from({ year: 2021, month: 13 }, { overflow: "reject" });
// RangeError: date value "month" not in 1..12: 13
| Specification |
|---|
| Temporal> # sec-temporal.plainyearmonth.from> |
| Desktop | Mobile | Server | |||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | WebView on iOS | Bun | Deno | Node.js | |
from |
144 | 144 | 139 | No | No | 144 | 139 | No | No | No | 144 | No | ? | 1.40 | No |
© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainYearMonth/from