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 |
瀏覽器相容性
載入中…