> ## Documentation Index
> Fetch the complete documentation index at: https://bun.zhcndoc.com/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/bun-zhcndoc/cnUTwgMuf4cCrwC-/icons/typescript.svg?fit=max&auto=format&n=cnUTwgMuf4cCrwC-&q=85&s=e7767043c9e885c34f2d6c8fe2a95217" theme={"theme":{"light":"github-light","dark":"dracula"}}
process.env.TZ = "America/New_York";
```

***

或者，你也可以在运行 Bun 命令时，从命令行设置该环境变量。

```sh terminal icon="terminal" theme={"theme":{"light":"github-light","dark":"dracula"}}
TZ=America/New_York bun run dev
```

***

一旦设置了 `TZ`，所有的 `Date` 实例将使用该时区。默认情况下，所有日期都使用系统配置的时区。

```ts process.ts icon="https://mintcdn.com/bun-zhcndoc/cnUTwgMuf4cCrwC-/icons/typescript.svg?fit=max&auto=format&n=cnUTwgMuf4cCrwC-&q=85&s=e7767043c9e885c34f2d6c8fe2a95217" theme={"theme":{"light":"github-light","dark":"dracula"}}
new Date().getHours(); // => 18

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

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