Temporal.PlainMonthDay.prototype.with()

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 with() method of Temporal.PlainMonthDay instances returns a new Temporal.PlainMonthDay object representing this month-day with some fields replaced by new values. Because all Temporal objects are designed to be immutable, this method essentially functions as the setter for the month-day's fields.

There's no obvious way to create a new Temporal.PlainMonthDay object that represents the same month-day in a different calendar, so to replace its calendarId property, you need to convert it to a Temporal.PlainDate object using toPlainDate(), change the calendar, and then convert it back.

Syntax

js
with(info)
with(info, options)

Parameters

info

An object containing at least one of the properties recognized by Temporal.PlainMonthDay.from() (except calendar): day, era and eraYear, month, monthCode, year. Unspecified properties use the values from the original month-day. You need to provide the year if and only if you provide month and the calendar is not iso8601. You only need to provide one of month or monthCode, and one of era and eraYear or year, and the other will be updated accordingly.

options Optional

An object containing the following property:

overflow Optional

A string specifying the behavior when a date component is out of range. Possible values are:

"constrain" (default)

The date component is clamped to the valid range.

"reject"

A RangeError is thrown if the date component is out of range.

Return value

A new Temporal.PlainMonthDay object, where the fields specified in info that are not undefined are replaced by the corresponding values, and the rest of the fields are copied from the original date.

Exceptions

TypeError

Thrown in one of the following cases:

  • info is not an object.
  • options is not an object or undefined.
RangeError

Thrown in one of the following cases:

  • The provided properties that specify the same component are inconsistent.
  • The provided non-numerical properties are not valid; for example, if monthCode is never a valid month code in this calendar.
  • The provided numerical properties are out of range, and options.overflow is set to "reject".
  • If the year is provided, the calendar is not iso8601, and the year is not in the representable range or years, which is from -271821 to 275760.

Examples

Using with()

js
const md = Temporal.PlainMonthDay.from("07-01");
const newMd = md.with({ day: 2 });
console.log(newMd.toString()); // "07-02"

For more examples, see the documentation for the individual properties that can be set using with().

Specifications

Specification
Temporal proposal
# sec-temporal.plainmonthday.prototype.with

Browser compatibility

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
with
Experimental

Legend

Tip: you can click/tap on a cell for more information.

No support
No support
Experimental. Expect behavior to change in the future.
See implementation notes.
User must explicitly enable this feature.

See also