Temporal.PlainYearMonth
En esta página
The Temporal.PlainYearMonth object represents the year and month of a calendar date, without a day or time zone; for example, an event on a calendar that happens during the whole month. It is fundamentally represented as an ISO 8601 calendar date, with year, month, and day fields, and an associated calendar system. The day is used to disambiguate the year-month in non-ISO calendar systems.
Description#
A PlainYearMonth is essentially the year-month part of a {{jsxref("Temporal.PlainDate")}} object, without the day.
RFC 9557 format#
PlainYearMonth objects can be serialized and parsed using the RFC 9557 format, an extension to the ISO 8601 / RFC 3339 format. The string has the following form (spaces are only for readability and should not be present in the actual string):
YYYY-MM-DD [u-ca=calendar_id]
YYYY- : Either a four-digit number, or a six-digit number with a
+or-sign. MM- : A two-digit number from
01to12. DD{{optional_inline}}- : A two-digit number from
01to31. It is required for non-ISO calendars, and optional otherwise. If omitted, the string looks likeYYYY-MMorYYYYMM. Note that the reference day actually stored may be different from the one you provide, but the represented year-month is the same. See {{jsxref("Temporal/PlainYearMonth/from", "Temporal.PlainYearMonth.from()")}} for more information. TheYYYY,MM, andDDcomponents can be separated by-or nothing. [u-ca=calendar_id]{{optional_inline}}- : Replace
calendar_idwith the calendar to use. SeeIntl.supportedValuesOf()for a list of commonly supported calendar types. Defaults to[u-ca=iso8601]. May have a critical flag by prefixing the key with!: e.g.,[!u-ca=iso8601]. This flag generally tells other systems that it cannot be ignored if they don't support it. TheTemporalparser will throw an error if the annotations contain two or more calendar annotations and one of them is critical. Note that theYYYY-MM-DDis always interpreted as an ISO 8601 calendar date and then converted to the specified calendar.
As an input, you may optionally include the time, offset, and time zone identifier, in the same format as PlainDateTime, but they will be ignored. Other annotations in the [key=value] format are also ignored, and they must not have the critical flag.
When serializing, you can configure whether to display the calendar ID, and whether to add a critical flag for it.
Constructor#
- {{jsxref("Temporal/PlainYearMonth/PlainYearMonth", "Temporal.PlainYearMonth()")}} {{experimental_inline}}
- : Creates a new
Temporal.PlainYearMonthobject by directly supplying the underlying data.
Static methods#
- {{jsxref("Temporal/PlainYearMonth/compare", "Temporal.PlainYearMonth.compare()")}}
- : Returns a number (-1, 0, or 1) indicating whether the first year-month comes before, is the same as, or comes after the second year-month. Equivalent to comparing their underlying ISO 8601 dates. Two year-months from different calendars may be considered equal if they start on the same ISO date.
- {{jsxref("Temporal/PlainYearMonth/from", "Temporal.PlainYearMonth.from()")}}
- : Creates a new
Temporal.PlainYearMonthobject from anotherTemporal.PlainYearMonthobject, an object with year and month properties, or an RFC 9557 string.
Instance properties#
These properties are defined on Temporal.PlainYearMonth.prototype and shared by all Temporal.PlainYearMonth instances.
- {{jsxref("Temporal/PlainYearMonth/calendarId", "Temporal.PlainYearMonth.prototype.calendarId")}}
- : Returns a string representing the calendar used to interpret the internal ISO 8601 date.
- {{jsxref("Object/constructor", "Temporal.PlainYearMonth.prototype.constructor")}}
- : The constructor function that created the instance object. For
Temporal.PlainYearMonthinstances, the initial value is the {{jsxref("Temporal/PlainYearMonth/PlainYearMonth", "Temporal.PlainYearMonth()")}} constructor. - {{jsxref("Temporal/PlainYearMonth/daysInMonth", "Temporal.PlainYearMonth.prototype.daysInMonth")}}
- : Returns a positive integer representing the number of days in the month of this date. Calendar-dependent.
- {{jsxref("Temporal/PlainYearMonth/daysInYear", "Temporal.PlainYearMonth.prototype.daysInYear")}}
- : Returns a positive integer representing the number of days in the year of this date. Calendar-dependent. For the ISO 8601 calendar, this is 365, or 366 in a leap year.
- {{jsxref("Temporal/PlainYearMonth/era", "Temporal.PlainYearMonth.prototype.era")}}
- : Returns a calendar-specific lowercase string representing the era of this year-month, or
undefinedif the calendar does not use eras (e.g., ISO 8601).eraanderaYeartogether uniquely identify a year in a calendar, in the same way thatyeardoes. Calendar-dependent. For Gregorian, it is either"ce"or"bce". - {{jsxref("Temporal/PlainYearMonth/eraYear", "Temporal.PlainYearMonth.prototype.eraYear")}}
- : Returns a non-negative integer representing the year of this year-month within the era, or
undefinedif the calendar does not use eras (e.g., ISO 8601). The year index usually starts from 1 (more common) or 0, and years in an era can decrease with time (e.g., Gregorian BCE).eraanderaYeartogether uniquely identify a year in a calendar, in the same way thatyeardoes. Calendar-dependent. - {{jsxref("Temporal/PlainYearMonth/inLeapYear", "Temporal.PlainYearMonth.prototype.inLeapYear")}}
- : Returns a boolean indicating whether this year-month is in a leap year. A leap year is a year that has more days (due to a leap day or leap month) than a common year. Calendar-dependent.
- {{jsxref("Temporal/PlainYearMonth/month", "Temporal.PlainYearMonth.prototype.month")}}
- : Returns a positive integer representing the 1-based month index in the year of this year-month. The first month of this year is
1, and the last month is the {{jsxref("Temporal/PlainYearMonth/monthsInYear", "monthsInYear")}}. Calendar-dependent. Note that unlike {{jsxref("Date.prototype.getMonth()")}}, the index is 1-based. If the calendar has leap months, then the month with the same {{jsxref("Temporal/PlainDate/monthCode", "monthCode")}} may have differentmonthindexes for different years. - {{jsxref("Temporal/PlainYearMonth/monthCode", "Temporal.PlainYearMonth.prototype.monthCode")}}
- : Returns a calendar-specific string representing the month of this year-month. Calendar-dependent. Usually it is
Mplus a two-digit month number. For leap months, it is the previous month's code followed byL. If the leap month is the first month of the year, the code isM00L. - {{jsxref("Temporal/PlainYearMonth/monthsInYear", "Temporal.PlainYearMonth.prototype.monthsInYear")}}
- : Returns a positive integer representing the number of months in the year of this date. Calendar-dependent. For the ISO 8601 calendar, this is always 12, but in other calendar systems it may differ.
- {{jsxref("Temporal/PlainYearMonth/year", "Temporal.PlainYearMonth.prototype.year")}}
- : Returns an integer representing the number of years of this year-month relative to the start of a calendar-specific epoch year. Calendar-dependent. Usually year 1 is either the first year of the latest era or the ISO 8601 year
0001. If the epoch is in the middle of the year, that year will have the same value before and after the start date of the era. Temporal.PlainYearMonth.prototype[Symbol.toStringTag]- : The initial value of the
[Symbol.toStringTag]property is the string"Temporal.PlainYearMonth". This property is used in {{jsxref("Object.prototype.toString()")}}.
Instance methods#
- {{jsxref("Temporal/PlainYearMonth/add", "Temporal.PlainYearMonth.prototype.add()")}}
- : Returns a new
Temporal.PlainYearMonthobject representing this year-month moved forward by a given duration (in a form convertible by {{jsxref("Temporal/Duration/from", "Temporal.Duration.from()")}}). - {{jsxref("Temporal/PlainYearMonth/equals", "Temporal.PlainYearMonth.prototype.equals()")}}
- : Returns
trueif this year-month is equivalent in value to another year-month (in a form convertible by {{jsxref("Temporal/PlainYearMonth/from", "Temporal.PlainYearMonth.from()")}}), andfalseotherwise. They are compared both by their underlying ISO date values and their calendars, so two year-months from different calendars may be considered equal by {{jsxref("Temporal/PlainYearMonth/compare", "Temporal.PlainYearMonth.compare()")}} but not byequals(). - {{jsxref("Temporal/PlainYearMonth/since", "Temporal.PlainYearMonth.prototype.since()")}}
- : Returns a new {{jsxref("Temporal.Duration")}} object representing the duration from another year-month (in a form convertible by {{jsxref("Temporal/PlainYearMonth/from", "Temporal.PlainYearMonth.from()")}}) to this year-month. The duration is positive if the other month is before this month, and negative if after.
- {{jsxref("Temporal/PlainYearMonth/subtract", "Temporal.PlainYearMonth.prototype.subtract()")}}
- : Returns a new
Temporal.PlainYearMonthobject representing this year-month moved backward by a given duration (in a form convertible by {{jsxref("Temporal/Duration/from", "Temporal.Duration.from()")}}). - {{jsxref("Temporal/PlainYearMonth/toJSON", "Temporal.PlainYearMonth.prototype.toJSON()")}}
- : Returns a string representing this year-month in the same RFC 9557 format as calling {{jsxref("Temporal/PlainYearMonth/toString", "toString()")}}. Intended to be implicitly called by {{jsxref("JSON.stringify()")}}.
- {{jsxref("Temporal/PlainYearMonth/toLocaleString", "Temporal.PlainYearMonth.prototype.toLocaleString()")}}
- : Returns a string with a language-sensitive representation of this year-month.
- {{jsxref("Temporal/PlainYearMonth/toPlainDate", "Temporal.PlainYearMonth.prototype.toPlainDate()")}}
- : Returns a new {{jsxref("Temporal.PlainDate")}} object representing this year-month and a supplied day in the same calendar system.
- {{jsxref("Temporal/PlainYearMonth/toString", "Temporal.PlainYearMonth.prototype.toString()")}}
- : Returns a string representing this year-month in the RFC 9557 format.
- {{jsxref("Temporal/PlainYearMonth/until", "Temporal.PlainYearMonth.prototype.until()")}}
- : Returns a new {{jsxref("Temporal.Duration")}} object representing the duration from this year-month to another year-month (in a form convertible by {{jsxref("Temporal/PlainYearMonth/from", "Temporal.PlainYearMonth.from()")}}). The duration is positive if the other month is after this month, and negative if before.
- {{jsxref("Temporal/PlainYearMonth/valueOf", "Temporal.PlainYearMonth.prototype.valueOf()")}}
- : Throws a {{jsxref("TypeError")}}, which prevents
Temporal.PlainYearMonthinstances from being implicitly converted to primitives when used in arithmetic or comparison operations. - {{jsxref("Temporal/PlainYearMonth/with", "Temporal.PlainYearMonth.prototype.with()")}}
- : Returns a new
Temporal.PlainYearMonthobject representing this year-month with some fields replaced by new values.
Specifications#
{{Specifications}}
Browser compatibility#
{{Compat}}
See also#
- {{jsxref("Temporal")}}
- {{jsxref("Temporal.PlainDate")}}
- {{jsxref("Temporal.PlainMonthDay")}}