Temporal.PlainMonthDay.prototype.toString()

可用性有限

此特性不是基線特性,因為它在一些最廣泛使用的瀏覽器中不起作用。

實驗性: 這是一項實驗性技術
在生產中使用此技術之前,請仔細檢查瀏覽器相容性表格

toString() 方法 Temporal.PlainMonthDay 例項返回一個字串,表示此月份-日期,採用 RFC 9557 格式

語法

js
toString()
toString(options)

引數

options 可選

包含以下屬性的物件

calendarName 可選

是否在返回值中顯示日曆註釋 ([u-ca=calendar_id])。可能的值是:

"auto"(預設值)

如果日曆不是 "iso8601",則包含日曆註解。如果日曆不是 "iso8601",則包含參考年份。

"always"

始終包含日曆註解。參考年份也始終包含。

"never"

從不包含日曆註解。這使得返回的字串無法恢復到同一個 Temporal.PlainMonthDay 例項,儘管月份-日期的值保持不變。如果日曆不是 "iso8601",則包含參考年份。

"critical"

始終包含日曆註解,並新增一個關鍵標誌:[!u-ca=calendar_id]。在將字串傳送到某些系統時很有用,但對 Temporal 本身無用。參考年份也始終包含。

返回值

一個採用 RFC 9557 格式 表示此月份-日期的字串。按照規定包含日曆註解。如果包含日曆註解或日曆不是 "iso8601",則包含參考年份。

異常

RangeError

如果任何選項無效,則丟擲。

TypeError

如果 options 不是物件或 undefined,則丟擲錯誤。

示例

使用 toString()

js
const md = Temporal.PlainMonthDay.from({ month: 8, day: 1 });
console.log(md.toString()); // '08-01'

const md2 = Temporal.PlainMonthDay.from({
  monthCode: "M08",
  day: 1,
  calendar: "chinese",
});
console.log(md2.toString()); // '1972-09-08[u-ca=chinese]'

使用選項

js
const isoMD = Temporal.PlainMonthDay.from({ month: 8, day: 1 });
const md = Temporal.PlainMonthDay.from({
  monthCode: "M08",
  day: 1,
  calendar: "chinese",
});
console.log(isoMD.toString({ calendarName: "auto" })); // '08-01'
console.log(md.toString({ calendarName: "auto" })); // '1972-09-08[u-ca=chinese]'
console.log(isoMD.toString({ calendarName: "always" })); // '1972-08-01[u-ca=iso8601]'
console.log(md.toString({ calendarName: "always" })); // '1972-09-08[u-ca=chinese]'
console.log(isoMD.toString({ calendarName: "never" })); // '08-01'
console.log(md.toString({ calendarName: "never" })); // '1972-09-08'
console.log(isoMD.toString({ calendarName: "critical" })); // '1972-08-01[!u-ca=iso8601]'
console.log(md.toString({ calendarName: "critical" })); // '1972-09-08[!u-ca=chinese]'

規範

規範
Temporal
# sec-temporal.plainmonthday.prototype.tostring

瀏覽器相容性

另見