Resource details · 实用工具
同一时刻的双时区时钟
把一个明确的UTC时刻转换成墨尔本与上海的当地时间,交给Intl处理各自的时区规则。
Resource text, examples and licences retain their original language. Code is displayed only; this page does not execute it or call AI.
Resource details
适合学习UTC、IANA时区和夏令时。示例时刻固定,时区必须由用户或可信设置提供;不会读取GPS、IP定位或自动推断用户所在城市。
Before you start
- Platforms
- Windows · macOS · Linux
- Content updated
- 准备Node.js 24和文本编辑器;本站仅在Node.js v24.19.0本地核对。
- 准备明确UTC时刻与IANA时区名称。
node timezone-clock.mjs先在文件所在目录打开终端。无需 npm install;本例不联网、不读写用户文件,不能直接代替生产平台接入。
Common pitfalls
- 按指定.mjs文件名保存,避免变成.mjs.txt;不需要npm install。
- 时区字段不是定位;演示时间固定,不会自动跟随旅行。
Complete resource
// SPDX-License-Identifier: MIT
// 原创教学示例:仅接受1000至9999年UTC ISO;IANA时区由调用方填写,不定位。
export function formatZonedTime(instantISO, timeZone) {
if (typeof instantISO !== "string" || !/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{3})?Z$/.test(instantISO)) throw new TypeError("请输入UTC ISO时间,例如2026-10-22T00:30:00Z");
if (Number(instantISO.slice(0, 4)) < 1000) throw new RangeError("教学时钟仅支持1000至9999年的UTC输入");
const milliseconds = Date.parse(instantISO);
if (!Number.isFinite(milliseconds)) throw new RangeError("时间无效");
const canonical = new Date(milliseconds).toISOString();
const normalized = instantISO.includes(".") ? instantISO : instantISO.replace("Z", ".000Z");
if (canonical !== normalized) throw new RangeError("不存在的日期或时间");
if (typeof timeZone !== "string" || timeZone.length > 80 || !(timeZone === "UTC" || timeZone === "GMT" || /^[A-Za-z_]+(\/[A-Za-z0-9_+-]+)+$/.test(timeZone))) throw new TypeError("请填写IANA时区名称");
let formatter;
try {
formatter = new Intl.DateTimeFormat("en-CA", {
timeZone, year: "numeric", month: "2-digit", day: "2-digit",
hour: "2-digit", minute: "2-digit", second: "2-digit", hourCycle: "h23",
});
} catch {
throw new RangeError("此运行环境不支持这个IANA时区");
}
const parts = new Map(formatter.formatToParts(new Date(milliseconds)).map(part => [part.type, part.value]));
const date = [parts.get("year").padStart(4, "0"), parts.get("month"), parts.get("day")].join("-");
const time = [parts.get("hour"), parts.get("minute"), parts.get("second")].join(":");
return { timeZone: formatter.resolvedOptions().timeZone, localTime: date + " " + time };
}
const instant = "2026-10-22T00:30:00Z";
console.log(JSON.stringify({
Melbourne: formatZonedTime(instant, "Australia/Melbourne").localTime,
Shanghai: formatZonedTime(instant, "Asia/Shanghai").localTime,
}));How to use it
把同一个固定UTC时刻展示为两地时间,不把当前电脑所在时区当成用户位置。
保存完整教学代码
点击下载代码,或复制正文保存为timezone-clock.mjs。保留许可说明;不要混入Markdown三反引号,确认后缀不是.mjs.txt。
在文件夹中运行
准备Node.js 24,在保存文件的目录打开终端,执行下方node命令;不需要账号、密钥或第三方依赖。
核对结果后再修改
此固定样例应显示墨尔本11:30、上海08:30。换时间时保留UTC的Z标记;更换城市时使用对应IANA时区。
- Save as
timezone-clock.mjs- Environment
- Node.js 24;本站运行核验为v24.19.0。保存为.mjs,无需npm install。
- Dependencies and costs
- 代码不联网、不调用模型、不读写用户文件;无接口费用。设备和电力成本由使用者承担,接入实际应用后的成本另行核对。
Command
node timezone-clock.mjsExpected output
{"Melbourne":"2026-10-22 11:30:00","Shanghai":"2026-10-22 08:30:00"}Local verification
2026-10-22 · Node.js v24.19.0
已在本站本机运行此页原样附带的教学代码,并核对演示输出及本批注明的边界测试;不代表生产验证、全平台实测或安全审计。
Suitable uses
- 多时区时间展示
- 区分UTC时间、当地时间与位置来源
Before using it
- 固定日期是演示样例,不是实时钟表。时区输入不是定位,旅行后需由用户明确更改设置。
- 教学时钟只接受1000至9999年的UTC ISO输入及有效IANA名称,例如Australia/Melbourne、Asia/Shanghai;不支持低年份或含糊城市昵称。
- 夏令时取决于该日期及运行环境自带的时区数据库;未来规则可能变化,长期部署需维护运行环境。
Authors and sources
- Attribution
- 包子海梦 · AI辅助开发
- Authors and sources
- 提示集贡献者
Notices and modifications
新编写的通用教学示例,发布前仍需核对来源与运行效果。
Complete licence
MIT
MIT License Copyright (c) 2026 提示集贡献者 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Verification scope
教学代码已运行
已运行完整带许可的教学文件并核对输出;不代表生产部署、平台接入或全部边界情况都已验证。
2026-10-23 Node.js v24.19.0