Date.prototype.toTemporalInstant()
Limited availability
This feature is not Baseline because it does not work in some of the most widely-used browsers.
Date
实例的 toTemporalInstant()
方法返回一个新的 Temporal.Instant
对象,该对象的 epochMilliseconds
值与当前日期的时间戳 相同。
可以使用此方法将传统的 Date
值转换为 Temporal
API 格式,然后根据需要进一步将其转换为其他 Temporal
类。
语法
js
toTemporalInstant()
参数
无。
返回值
一个新的 Temporal.Instant
对象,其 epochMilliseconds
值与当前日期的时间戳相同。其的微秒和纳秒部分始终是 0
。
异常
RangeError
-
如果日期无效(其时间戳为
NaN
)则抛出此异常。
示例
>使用 toTemporalInstant()
js
const legacyDate = new Date("2021-07-01T12:34:56.789Z");
const instant = legacyDate.toTemporalInstant();
// 进一步将其转换为其他对象
const zdt = instant.toZonedDateTimeISO("UTC");
const date = zdt.toPlainDate();
console.log(date.toString()); // 2021-07-01
规范
Specification |
---|
Temporal> # sec-date.prototype.totemporalinstant> |
浏览器兼容性
Loading…