Temporal.ZonedDateTime.prototype.equals()
Temporal.ZonedDateTime
实例的 equals()
方法如果此日期-时间的值与另一个日期-时间(以可由 Temporal.ZonedDateTime.from()
转换的形式)等效,则返回 true
,否则返回 false
。它们通过其瞬时值、时区和日历进行比较,因此来自不同日历或时区的两个日期-时间可能被 Temporal.ZonedDateTime.compare()
视为相等,但不会被 equals()
视为相等。
语法
js
equals(other)
参数
其他
-
表示要比较的另一个日期-时间的字符串、对象或
Temporal.ZonedDateTime
实例。它使用与Temporal.ZonedDateTime.from()
相同的算法转换为Temporal.ZonedDateTime
对象。
返回值
如果此日期-时间在瞬时值、时区和日历上都等于 other
,则为 true
,否则为 false
。
请注意,时区在比较前会进行规范化,因此如果它们的时区 ID 都是命名且标识相同的时区,则即使精确名称可能是彼此的别名,它们也会被视为相同。偏移标识符通过它们表示的偏移值进行比较。偏移标识符从不与命名标识符比较相等,即使命名标识符的时区总是使用该偏移量。
示例
使用 equals()
js
// Asia/Kolkata and Asia/Calcutta are aliases of each other
const dt1 = Temporal.ZonedDateTime.from(
"2021-07-01T12:34:56+05:30[Asia/Kolkata]",
);
const dt2 = Temporal.ZonedDateTime.from(
"2021-07-01T12:34:56+05:30[Asia/Calcutta]",
);
console.log(dt1.equals(dt2)); // true
const dt3 = Temporal.ZonedDateTime.from("2021-07-01T12:34:56+05:30[+05:30]");
console.log(dt1.equals(dt3)); // false
const dt4 = Temporal.ZonedDateTime.from(
"2021-07-01T12:34:56+05:30[Asia/Kolkata][u-ca=buddhist]",
);
console.log(dt1.equals(dt4)); // false
测试两个时区标识符是否等效
js
function sameTimeZone(timeZone1, timeZone2) {
const dt1 = Temporal.ZonedDateTime.from({
year: 2021,
month: 7,
day: 1,
timeZone: timeZone1,
});
const dt2 = Temporal.ZonedDateTime.from({
year: 2021,
month: 7,
day: 1,
timeZone: timeZone2,
});
return dt1.equals(dt2);
}
console.log(sameTimeZone("Asia/Kolkata", "Asia/Calcutta")); // true
console.log(sameTimeZone("Asia/Shanghai", "Asia/Taipei")); // false
规范
规范 |
---|
Temporal # sec-temporal.zoneddatetime.prototype.equals |
浏览器兼容性
加载中…