Skip to main content
Bun 支持在 bun 进程的生命周期内以编程方式设置默认时区。要设置时区,请将 TZ 环境变量的值设置为一个有效的时区标识符
使用 bun 运行文件时,时区默认为系统配置的本地时区。使用 bun test 运行测试时,时区会被设置为 UTC,以使测试更具确定性。
https://mintcdn.com/ikxin/RzFFGbzo0-4huILA/icons/typescript.svg?fit=max&auto=format&n=RzFFGbzo0-4huILA&q=85&s=a3dffd2241f05776d3bd25171d0c5a79process.ts
process.env.TZ = "America/New_York";

或者,你也可以在运行 Bun 命令时,从命令行设置该环境变量。
terminal
TZ=America/New_York bun run dev

一旦设置了 TZ,所有的 Date 实例将使用该时区。默认情况下,所有日期都使用系统配置的时区。
https://mintcdn.com/ikxin/RzFFGbzo0-4huILA/icons/typescript.svg?fit=max&auto=format&n=RzFFGbzo0-4huILA&q=85&s=a3dffd2241f05776d3bd25171d0c5a79process.ts
new Date().getHours(); // => 18

process.env.TZ = "America/New_York";

new Date().getHours(); // => 21