
temporal-polyfill 入门指南PlainDate、PlainTime、ZonedDateTime 等 5 大日期类型一次讲透【免费下载链接】temporal-polyfillA lightweight polyfill for Temporal, successor to the JavaScript Date object项目地址: https://gitcode.com/gh_mirrors/tempo/temporalJavaScript 的Date对象饱受诟病已久月份从 0 开始、时区处理混乱、不可变操作缺失。而temporal-polyfill正是为此而生——它是 JavaScript 官方TemporalAPI 的轻量级实现被誉为Date的继任者让你在浏览器和 Node.js 中提前用上现代化的日期处理能力。本文是temporal-polyfill 入门指南带你一次搞懂PlainDate、PlainTime、PlainDateTime、ZonedDateTime、Instant这 5 大核心日期类型的使用方法。为什么你需要 temporal-polyfillDate对象的痛点几乎人人皆知月份从 0 开始计数new Date(2026, 5, 1)其实是 6 月 1 日日期是可变的setDate()会直接修改原对象容易引发隐晦 bug没有纯日期概念想表达2026-06-01只能硬凑时区处理全靠手动换算夏令时更是噩梦Temporal 标准从语言层面解决了这些问题而temporal-polyfill让你今天就能用上它。整个库仅19.5 kBgzip 后与规范高度一致是市面上最轻量的 Temporal polyfill 之一。最快上手方法一条命令安装安装只需一条命令npm install temporal-polyfill然后在代码入口引入全局补丁import temporal-polyfill/global Temporal.Now.zonedDateTimeISO().toString() // 2026-06-18T13:45:00-04:00[America/New_York]如果浏览器原生支持 Temporalpolyfill 会自动让位给原生实现无需任何额外配置。若想按需安装也可以使用temporal-polyfill/shim入口在满足自定义条件时才注入全局。5 大日期类型一次讲透Temporal 把时间拆分成不同的类型各司其职。下面逐个介绍 5 大核心日期类型。1. PlainDate纯粹的日期不含时间Temporal.PlainDate只表示某年某月某日没有任何时区和时刻的概念非常适合生日、纪念日、排期这类场景。const date Temporal.PlainDate.from(2026-06-01) date.dayOfWeek // 1周一 date.daysInMonth // 30 date.add({ days: 10 }).toString() // 2026-06-11 date.with({ month: 12 }).toString() // 2026-12-01它天然不可变——add、with都返回新对象原对象保持不变彻底告别Date的副作用问题。2. PlainTime纯粹的时间不含日期Temporal.PlainTime表示一天中的某个时刻如14:30:00同样不绑定任何日期或时区适合闹钟、营业时间、倒计时这类需求。const time Temporal.PlainTime.from(14:30:00) time.hour // 14 time.add({ minutes: 45 }).toString() // 15:15:00 time.round({ smallestUnit: hour }).toString() // 15:00:00对时间的加减、取整、比较API 设计都直观清晰不再需要手动换算成毫秒。3. PlainDateTime日期 时间但无时区Temporal.PlainDateTime是日历时间例如2026-06-01 14:30:00但它不知道自己属于哪个时区。它适合记录本地事件或在与时区组合前作为中间形态。const dt Temporal.PlainDateTime.from(2026-06-01T14:30:00) dt.toPlainDate().toString() // 2026-06-01 dt.toPlainTime().toString() // 14:30:00 dt.toZonedDateTime(Asia/Shanghai).toString() // 2026-06-01T14:30:0008:00[Asia/Shanghai]4. ZonedDateTime带时区的完整时间最常用Temporal.ZonedDateTime是日常开发中最常用的类型它把时刻 时区 日历整合在一起自动处理夏令时、时区偏移等棘手问题。const zdt Temporal.ZonedDateTime.from(2026-06-01T14:30:00[Asia/Shanghai]) zdt.offset // 08:00 zdt.withTimeZone(America/New_York).toString() // 2026-06-01T02:30:00-04:00[America/New_York]跨时区换算只需一行代码夏令时切换也由库自动处理这是Date对象完全无法企及的能力。5. Instant精确到纳秒的时间戳Temporal.Instant表示绝对时刻与任何时区无关本质是一个纳秒精度的时间点适合日志记录、数据存储、接口通信。const instant Temporal.Instant.from(2026-06-01T06:30:00Z) instant.epochMilliseconds // 1780295400000 instant.toZonedDateTimeISO(Asia/Shanghai).toString() // 2026-06-01T14:30:0008:00[Asia/Shanghai]Instant可以与ZonedDateTime无缝互转是时间数据的底层存储格式。5 大日期类型速查表类型含义适用场景典型输出PlainDate纯日期生日、排期2026-06-01PlainTime纯时间闹钟、营业时间14:30:00PlainDateTime日期 时间本地事件记录2026-06-01T14:30:00ZonedDateTime带时区的完整时间跨时区业务2026-06-01T14:30:0008:00[Asia/Shanghai]Instant绝对时刻存储、日志2026-06-01T06:30:00Z选型的口诀很简单只要涉及时区就用ZonedDateTime只存日志/时间戳用Instant其余场景按需选用三种Plain类型。实际场景一个 5 行的跨时区会议提醒把上面的知识串起来看看真实场景有多简洁const meeting Temporal.ZonedDateTime.from(2026-06-01T14:30:00[Asia/Shanghai]) const reminder meeting.subtract({ minutes: 15 }) console.log(reminder.withTimeZone(America/New_York).toString()) // 2026-06-01T01:15:00-04:00[America/New_York]时区转换、时间计算、夏令时处理全部自动完成代码却只有 3 行可读性远超Date时代的写法。进阶包体积敏感试试 Tree-shakeable APItemporal-polyfill 还额外提供了一套可摇树优化tree-shakeable的函数式 API每个操作都是独立函数打包器只保留你真正用到的部分包体积进一步压缩。对应源码位于项目的polyfill/src/目录下如polyfill/src/classApi/basic/plainDate.ts、polyfill/src/classApi/basic/zonedDateTime.ts文档则集中在docs/fns/中如docs/fns/plaindate.md、docs/fns/zoneddatetime.md。import * as PlainDateFns from temporal-polyfill/fns/PlainDate const date PlainDateFns.create(2026, 6, 1) PlainDateFns.addMonths(date, 2).toString() // 2026-08-01如果将来你的目标环境全部支持原生 Temporal还可以借助项目自带的 codemod 工具codemod/目录自动把函数式调用改写成标准的Temporal.*语法平滑迁移、零锁定。总结temporal-polyfill 让 JavaScript 开发者告别Date对象的历史包袱PlainDate、PlainTime、PlainDateTime、ZonedDateTime、Instant5 大日期类型覆盖了从纯日期到跨时区完整时刻的全部场景。现在安装它你今天就能以官方标准的方式优雅地处理时间。对源码感兴趣的话也可以直接 clone 本仓库https://gitcode.com/gh_mirrors/tempo/temporal研究其实现。【免费下载链接】temporal-polyfillA lightweight polyfill for Temporal, successor to the JavaScript Date object项目地址: https://gitcode.com/gh_mirrors/tempo/temporal创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考