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

js
Temporal.PlainMonthDay.from(info)
Temporal.PlainMonthDay.from(info, options)

Parameters

info

One of the following:

  • A Temporal.PlainMonthDay instance, 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):

    calendar Optional

    A string that corresponds to the calendarId property. Defaults to "iso8601". All other properties are interpreted in this calendar system (unlike the Temporal.PlainMonthDay() constructor, which interprets the values in the ISO calendar system).

    day

    An integer that corresponds to the day property. Must be positive regardless of the overflow option.

    era and eraYear

    A string and an integer that can be used instead of year. See era and eraYear of PlainDate. Are only used if the calendar system has eras. era and eraYear must be provided simultaneously. If month is specified, at least one of era+eraYear or year must be provided. If all of era, eraYear, and year are provided, they must be consistent.

    month

    A positive integer that can be used instead of monthCode. See month of PlainDate. Must be positive regardless of the overflow option. If month is provided, and the calendar is not iso8601, then year (or era + eraYear as a substitution) must be provided too, because the same month may map to multiple possible monthCode values in different years. At least one of month or monthCode must be provided. If both month and monthCode are provided, they must be consistent.

    monthCode

    Corresponds to the monthCode property. At least one of month or monthCode must be provided. If both month and monthCode are provided, they must be consistent.

    year

    An integer used to disambiguate month if provided, because for some calendars, the same month can mean different monthCode in different years. See year of PlainDate. If a year is provided, then the overflow option validates the month-day in the given year, not just any year. If month is specified, at least one of era+eraYear or year must be provided. If all of era, eraYear, and year are provided, they must be consistent.

options Optional

An object containing the following property:

overflow Optional

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 RangeError is 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:

  • info is not an object or a string.
  • options is not an object or undefined.
  • The provided properties are insufficient to unambiguously determine a date. You usually need to provide a year (or era and eraYear), a month, and a day, or a monthCode and a day.
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 monthCode is never a valid month code in this calendar.
  • The provided numerical properties are out of range, and options.overflow is set to "reject".

Examples

Creating a PlainMonthDay from an object

js
// 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/month combination is invalid, the month is clamped to obtain a valid monthCode in the year.
  • If the year/monthCode combination is invalid, a different year is chosen to keep the monthCode as-is.
  • The day is 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.

js
// 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:

js
Temporal.PlainMonthDay.from(
  { year: 2021, month: 13, day: 1 },
  { overflow: "reject" },
);
// RangeError: date value "month" not in 1..12: 13

Specifications

Specification
Temporal proposal
# sec-temporal.plainmonthday.from

Browser compatibility

BCD tables only load in the browser

See also