Temporal.PlainYearMonth.prototype.equals()

可用性有限

此特性不是基线特性,因为它在一些最广泛使用的浏览器中不起作用。

实验性: 这是一项实验性技术
在生产中使用此技术之前,请仔细检查浏览器兼容性表格

equals() 方法用于 Temporal.PlainYearMonth 实例,当此年月与其另一个年月在值上等效时(可由 Temporal.PlainYearMonth.from() 转换的形式),返回 true,否则返回 false。它们会同时根据其底层的 ISO 日期值和日历进行比较,因此来自不同日历的两个年月可能在 Temporal.PlainYearMonth.compare() 中被视为相等,但在 equals() 中不被视为相等。

注意: PlainYearMonth 对象会跟踪一个参考 ISO 日期,该日期也用于比较。当使用 Temporal.PlainYearMonth.from() 方法时,该日期会自动设置;但可以使用 Temporal.PlainYearMonth() 构造函数手动设置,这会导致两个等效的年月因参考日期不同而被视为不同。因此,您应该避免直接使用构造函数,而优先使用 from() 方法。

语法

js
equals(other)

参数

其他

要比较的另一个年月的字符串、对象或 Temporal.PlainYearMonth 实例。它使用与 Temporal.PlainYearMonth.from() 相同的算法转换为 Temporal.PlainYearMonth 对象。

返回值

如果此年月在日期值和日历上都等于 other,则返回 true,否则返回 false

示例

使用 equals()

js
const ym1 = Temporal.PlainYearMonth.from("2021-08");
const ym2 = Temporal.PlainYearMonth.from({ year: 2021, month: 8 });
console.log(ym1.equals(ym2)); // true

const ym3 = Temporal.PlainYearMonth.from("2021-08-01[u-ca=japanese]");
console.log(ym1.equals(ym3)); // false

const ym4 = Temporal.PlainYearMonth.from("2021-09");
console.log(ym1.equals(ym4)); // false

规范

规范
Temporal
# sec-temporal.plainyearmonth.prototype.equals

浏览器兼容性

另见