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 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.
- : Either a four-digit number, or a six-digit number with a
MM- : A two-digit number from
01to12.
- : A two-digit number from
DD- : 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 Temporal/PlainYearMonth/from for more information. TheYYYY,MM, andDDcomponents can be separated by-or nothing.
- : A two-digit number from
[u-ca=calendar_id]- : 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.
- : Replace
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
- Temporal/PlainYearMonth/PlainYearMonth
- : Creates a new
Temporal.PlainYearMonthobject by directly supplying the underlying data.
- : Creates a new
Static methods
- 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.
- Temporal/PlainYearMonth/from
- : Creates a new
Temporal.PlainYearMonthobject from anotherTemporal.PlainYearMonthobject, an object with year and month properties, or an RFC 9557 string.
- : Creates a new
Instance properties
These properties are defined on Temporal.PlainYearMonth.prototype and shared by all Temporal.PlainYearMonth instances.
- Temporal/PlainYearMonth/calendarId
- : Returns a string representing the calendar used to interpret the internal ISO 8601 date.
- Object/constructor
- : The constructor function that created the instance object. For
Temporal.PlainYearMonthinstances, the initial value is the Temporal/PlainYearMonth/PlainYearMonth constructor.
- : The constructor function that created the instance object. For
- Temporal/PlainYearMonth/daysInMonth
- : Returns a positive integer representing the number of days in the month of this date. Calendar-dependent.
- Temporal/PlainYearMonth/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.
- Temporal/PlainYearMonth/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".
- : Returns a calendar-specific lowercase string representing the era of this year-month, or
- Temporal/PlainYearMonth/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.
- : Returns a non-negative integer representing the year of this year-month within the era, or
- Temporal/PlainYearMonth/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.
- Temporal/PlainYearMonth/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 Temporal/PlainYearMonth/monthsInYear. Calendar-dependent. Note that unlike Date.prototype.getMonth(), the index is 1-based. If the calendar has leap months, then the month with the same Temporal/PlainDate/monthCode may have differentmonthindexes for different years.
- : Returns a positive integer representing the 1-based month index in the year of this year-month. The first month of this year is
- Temporal/PlainYearMonth/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.
- : Returns a calendar-specific string representing the month of this year-month. Calendar-dependent. Usually it is
- Temporal/PlainYearMonth/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.
- Temporal/PlainYearMonth/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.
- : 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
Temporal.PlainYearMonth.prototype[Symbol.toStringTag]- : The initial value of the
[Symbol.toStringTag]property is the string"Temporal.PlainYearMonth". This property is used in Object.prototype.toString().
- : The initial value of the
Instance methods
- Temporal/PlainYearMonth/add
- : Returns a new
Temporal.PlainYearMonthobject representing this year-month moved forward by a given duration (in a form convertible by Temporal/Duration/from).
- : Returns a new
- Temporal/PlainYearMonth/equals
- : Returns
trueif this year-month is equivalent in value to another year-month (in a form convertible by 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 Temporal/PlainYearMonth/compare but not byequals().
- : Returns
- Temporal/PlainYearMonth/since
- : Returns a new Temporal.Duration object representing the duration from another year-month (in a form convertible by Temporal/PlainYearMonth/from) to this year-month. The duration is positive if the other month is before this month, and negative if after.
- Temporal/PlainYearMonth/subtract
- : Returns a new
Temporal.PlainYearMonthobject representing this year-month moved backward by a given duration (in a form convertible by Temporal/Duration/from).
- : Returns a new
- Temporal/PlainYearMonth/toJSON
- : Returns a string representing this year-month in the same RFC 9557 format as calling Temporal/PlainYearMonth/toString. Intended to be implicitly called by JSON.stringify().
- Temporal/PlainYearMonth/toLocaleString
- : Returns a string with a language-sensitive representation of this year-month.
- Temporal/PlainYearMonth/toPlainDate
- : Returns a new Temporal.PlainDate object representing this year-month and a supplied day in the same calendar system.
- Temporal/PlainYearMonth/toString
- : Returns a string representing this year-month in the RFC 9557 format.
- Temporal/PlainYearMonth/until
- : Returns a new Temporal.Duration object representing the duration from this year-month to another year-month (in a form convertible by Temporal/PlainYearMonth/from). The duration is positive if the other month is after this month, and negative if before.
- Temporal/PlainYearMonth/valueOf
- : Throws a TypeError, which prevents
Temporal.PlainYearMonthinstances from being implicitly converted to primitives when used in arithmetic or comparison operations.
- : Throws a TypeError, which prevents
- Temporal/PlainYearMonth/with
- : Returns a new
Temporal.PlainYearMonthobject representing this year-month with some fields replaced by new values.
- : Returns a new
Specifications
Browser compatibility
See also
- Temporal
- Temporal.PlainDate
- Temporal.PlainMonthDay
来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。
