Open Playground
JS TutorialLesson #202

JavaScript PlainYearMonth

The Temporal.PlainYearMonth object is a year and month object.

The Temporal.PlainYearMonth Object

The Temporal.PlainYearMonth object is a year and month object.

It represents the year and month of an ISO 8601 calendar without day or time.

Example: 2026-05.


How to Create a PlainYearMonth Object

An PlainYearMonth object can be created in different ways:

FromCode
Parametersnew Temporal.PlainYearMonth(param)
ObjectTemporal.PlainYearMonth.from(object)
ISO / RFC 9557Temporal.PlainYearMonth.from(RFC 9557)

Create a PlainYearMonth with new

The new PlainYearMonth() constructor creates a new PlainYearMonth object with integers as input parameters.

Example

Example Code

javascript
Try it Yourself
// Create a PlainYearMonth object

const date = new Temporal.PlainYearMonth(2026, 5);

PlainYearMonth from(object)

The from() method creates a new PlainYearMonth object with an object as input parameter.

Example

Example Code

javascript
Try it Yourself
// Create a PlainYearMonth object

const date = new Temporal.PlainYearMonth({year:2026, month:5});

PlainYearMonth from(RFC 9557)

The from() method creates a new PlainYearMonth object with an ISO / RFC 9557 string as input parameter.

Examples

Example Code

javascript
Try it Yourself
// Create a PlainYearMonth object

const date = Temporal.PlainYearMonth.from("2025-05");
Note that the day input is ignored in the last example.

Temporal.PlainYearMonth Properties

Display All PlainYearMonth Properties

Example Code

javascript
Try it Yourself
const date = new Temporal.PlainYearMonth(2026, 5);

Convert to PlainTime

Example

Example Code

javascript
Try it Yourself
const yearMonth = new Temporal.PlainYearMonth(2026, 5);

const date = yearMonth.toPlainDate({ day: 17 });

PlainYearMonth add() and subtract()

The add() method accept a duration object as input.

Example

Example Code

javascript
Try it Yourself
// Create a new PlainYearMonth object

const d1 = Temporal.PlainYearMonth.from("2026-05");

// Add a duration

const d2 = d1.add({years:2, months:2})

All temporal objects have their own add() method:

  • duration.add(duration)
  • instant.add(duration)
  • plaindate.add(duration)
  • plaintime.add(duration)
  • plainyearmonth.add(duration)
  • plainmonthday.add(duration)
  • plaindatetime.add(duration)
  • zoneddatetime.add(duration)

All temporal objects have their own subtract() method:

  • duration.subtract(duration)
  • instant.subtract(duration)
  • plaindate.subtract(duration)
  • plaintime.subtract(duration)
  • plainyearmonth.subtract(duration)
  • plainmonthday.subtract(duration)
  • plaindatetime.subtract(duration)
  • zoneddatetime.subtract(duration)
Learn More: > JavaScript Temporal Arithmetic

The with() Method

The with() method returns a new PlainYearMonth object with specified field(s) replaced.

Example

Example Code

javascript
Try it Yourself
// Create a PlainYearMonth object

const date = Temporal.PlainYearMonth.from("2026-05");

// Replace month

const newDate = date.with({ month:12 });

All PlainYearMonth Methods

ConstructingDescription
from()Creates a PlainYearMonth object from an object or a string
new (constructor)Creates a PlainYearMonth object from integer parameters
Arithmetic
add()Returns a new PlainYearMonth with a duration added
subtract()Returns a new PlainYearMonth with a duration subtracted
Comparing
compare()Returns -1, 0, or 1 from comparing two dates
equals()Returns true if two dates are identical
since()Returns the difference since another date
until()Returns the difference until another date
Converting
toPlainDate()Returns a new PlainDate object
with()Returns a new PlainYearMonth with fields modified
Formatting
toJSON()Returns an RFC 9557 format string for JSON serialization
toLocaleString()Returns a language-sensitive representation of the date
toString()Returns an RFC 9557 format string representation
valueOf()TypeError (temporals cannot be converted to primitives)

All PlainYearMonth Properties

PropertyDescription
calendarIDCalendar system identifier ("iso8601")
daysInMonthThe total number of days in that month
daysInYearThe total number of days in that year
eraThe era name of the calendar, if applicable ("gregory")
eraYearThe year within the era, if applicable
inLeapYearA boolean indicating if the date falls in a leap year
monthThe month as an integer (1-12)
monthCodeA calendar-specific string code for the month ("M01")
monthsInYearThe total number of months in that year
yearThe year as an integer

Interactive Code Examples

JS Example 1

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
// Create a PlainYearMonth object

const date = new Temporal.PlainYearMonth(2026, 5);

JS Example 2

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
// Create a PlainYearMonth object

const date = new Temporal.PlainYearMonth({year:2026, month:5});

Examples

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
// Create a PlainYearMonth object

const date = Temporal.PlainYearMonth.from("2025-05");

Display All PlainYearMonth Properties

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
const date = new Temporal.PlainYearMonth(2026, 5);

JS Example 5

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
const yearMonth = new Temporal.PlainYearMonth(2026, 5);



const date = yearMonth.toPlainDate({ day: 17 });

JS Example 6

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
// Create a new PlainYearMonth object

const d1 = Temporal.PlainYearMonth.from("2026-05");



// Add a duration

const d2 = d1.add({years:2, months:2})