bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/JavaScript/Objects, Classes, and Advanced Patterns
JavaScript•Objects, Classes, and Advanced Patterns

JavaScript PlainYearMonth

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind JavaScript PlainYearMonth?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

// ___ a PlainYearMonth object
3Order

Put the learning moves in the order that makes the concept easiest to apply.

Create a PlainYearMonth with new
How to Create a PlainYearMonth Object
The Temporal.PlainYearMonth 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

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

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

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

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

Convert to PlainTime

Example

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

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

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

Previous

JavaScript Function bind()

Next

Self-Invoking Functions