Temporal.PlainYearMonth
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.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. MM
-
A two-digit number from
01
to12
. DD
Optional-
A two-digit number from
01
to31
. It is required for non-ISO calendars, and optional otherwise. If omitted, the string looks likeYYYY-MM
orYYYYMM
. Note that the reference day actually stored may be different from the one you provide, but the represented year-month is the same. SeeTemporal.PlainYearMonth.from()
for more information. TheYYYY
,MM
, andDD
components can be separated by-
or nothing. [u-ca=calendar_id]
Optional-
Replace
calendar_id
with the calendar to use. 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. TheTemporal
parser will throw an error if the annotations contain two or more calendar annotations and one of them is critical. Defaults to[u-ca=iso8601]
. Note that theYYYY-MM-DD
is 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
Temporal.PlainYearMonth()
Experimental-
Creates a new
Temporal.PlainYearMonth
object by directly supplying the underlying data.
Static methods
Temporal.PlainYearMonth.compare()
Experimental-
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()
Experimental-
Creates a new
Temporal.PlainYearMonth
object from anotherTemporal.PlainYearMonth
object, 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.
Temporal.PlainYearMonth.prototype.calendarId
Experimental-
Returns a string representing the calendar used to interpret the internal ISO 8601 date.
Temporal.PlainYearMonth.prototype.constructor
-
The constructor function that created the instance object. For
Temporal.PlainYearMonth
instances, the initial value is theTemporal.PlainYearMonth()
constructor. Temporal.PlainYearMonth.prototype.daysInMonth
Experimental-
Returns a positive integer representing the number of days in the month of this date. Calendar-dependent.
Temporal.PlainYearMonth.prototype.daysInYear
Experimental-
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.prototype.era
Experimental-
Returns a calendar-specific lowercase string representing the era of this year-month, or
undefined
if the calendar does not use eras (e.g. ISO 8601).era
anderaYear
together uniquely identify a year in a calendar, in the same way thatyear
does. Calendar-dependent. For Gregorian, it is either"gregory"
or"gregory-inverse"
. Temporal.PlainYearMonth.prototype.eraYear
Experimental-
Returns a non-negative integer representing the year of this year-month within the era, or
undefined
if 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).era
anderaYear
together uniquely identify a year in a calendar, in the same way thatyear
does. Calendar-dependent. Temporal.PlainYearMonth.prototype.inLeapYear
Experimental-
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.prototype.month
Experimental-
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 themonthsInYear
. Calendar-dependent. Note that unlikeDate.prototype.getMonth()
, the index is 1-based. If the calendar has leap months, then the month with the samemonthCode
may have differentmonth
indexes for different years. Temporal.PlainYearMonth.prototype.monthCode
Experimental-
Returns a calendar-specific string representing the month of this year-month. Calendar-dependent. Usually it is
M
plus 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
. Temporal.PlainYearMonth.prototype.monthsInYear
Experimental-
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.prototype.year
Experimental-
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 inObject.prototype.toString()
.
Instance methods
Temporal.PlainYearMonth.prototype.add()
Experimental-
Returns a new
Temporal.PlainYearMonth
object representing this year-month moved forward by a given duration (in a form convertible byTemporal.Duration.from()
). Temporal.PlainYearMonth.prototype.equals()
Experimental-
Returns
true
if this year-month is equivalent in value to another year-month (in a form convertible byTemporal.PlainYearMonth.from()
), andfalse
otherwise. They are compared both by their underlying ISO date values and their calendars, so two year-months from different calendars may be considered equal byTemporal.PlainYearMonth.compare()
but not byequals()
. Temporal.PlainYearMonth.prototype.since()
Experimental-
Returns a new
Temporal.Duration
object representing the duration from another year-month (in a form convertible byTemporal.PlainYearMonth.from()
) to this year-month. The duration is positive if the other month is before this month, and negative if after. Temporal.PlainYearMonth.prototype.subtract()
Experimental-
Returns a new
Temporal.PlainYearMonth
object representing this year-month moved backward by a given duration (in a form convertible byTemporal.Duration.from()
). Temporal.PlainYearMonth.prototype.toJSON()
Experimental-
Returns a string representing this year-month in the same RFC 9557 format as calling
toString()
. Intended to be implicitly called byJSON.stringify()
. Temporal.PlainYearMonth.prototype.toLocaleString()
Experimental-
Returns a string with a language-sensitive representation of this year-month.
Temporal.PlainYearMonth.prototype.toPlainDate()
Experimental-
Returns a new
Temporal.PlainDate
object representing this year-month and a supplied day in the same calendar system. Temporal.PlainYearMonth.prototype.toString()
Experimental-
Returns a string representing this year-month in the RFC 9557 format.
Temporal.PlainYearMonth.prototype.until()
Experimental-
Returns a new
Temporal.Duration
object representing the duration from this year-month to another year-month (in a form convertible byTemporal.PlainYearMonth.from()
). The duration is positive if the other month is after this month, and negative if before. Temporal.PlainYearMonth.prototype.valueOf()
Experimental-
Throws a
TypeError
, which preventsTemporal.PlainYearMonth
instances from being implicitly converted to primitives when used in arithmetic or comparison operations. Temporal.PlainYearMonth.prototype.with()
Experimental-
Returns a new
Temporal.PlainYearMonth
object representing this year-month with some fields replaced by new values.
Specifications
Specification |
---|
Temporal proposal # sec-temporal-plainyearmonth-objects |
Browser compatibility
BCD tables only load in the browser