> ## Documentation Index
> Fetch the complete documentation index at: https://bun.ll1025.cn/llms.txt
> Use this file to discover all available pages before exploring further.

# 在 Bun 中设置时区

Bun 支持以编程方式为 `bun` 进程的生命周期设置默认时区。将 `TZ` 环境变量设置为[有效的时区标识符](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones)。

<Note>
  使用 `bun` 运行文件时，时区默认为系统配置的本地时区。

  使用 `bun test` 运行测试时，时区设置为 `UTC`，以使测试更具确定性。
</Note>

```ts process.ts icon="https://mintcdn.com/span-inc/82N53aP7NFbaCVSl/icons/typescript.svg?fit=max&auto=format&n=82N53aP7NFbaCVSl&q=85&s=787d39d6a7d96d7f9540dc74344eba23" theme={null}
process.env.TZ = "America/New_York";
```

***

你也可以在运行 Bun 命令时在命令行上设置 `TZ`。

```sh terminal icon="terminal" theme={null}
TZ=America/New_York bun run dev
```

***

一旦设置了 `TZ`，每个 `Date` 实例都将使用该时区。

```ts process.ts icon="https://mintcdn.com/span-inc/82N53aP7NFbaCVSl/icons/typescript.svg?fit=max&auto=format&n=82N53aP7NFbaCVSl&q=85&s=787d39d6a7d96d7f9540dc74344eba23" theme={null}
new Date().getHours(); // => 18

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

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