> For the complete documentation index, see [llms.txt](https://docs.postgresql.tw/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.postgresql.tw/19/the-sql-language/datatype/datatype-datetime/datatype-datetime-input.md). Agents can also search and read this documentation through its [MCP server](https://docs.postgresql.tw/~gitbook/mcp).

# 8.5.1. 日期／時間輸入

## 8.5.1. 日期／時間輸入 [#](#datatype-datetime-input) <a href="#datatype-datetime-input" id="datatype-datetime-input"></a>

日期與時間輸入，幾乎能接受任何合理的格式，包括 ISO 8601、SQL 相容格式、 傳統的 POSTGRES 格式，以及其他格式。 對於某些格式而言，日期輸入中日、月、年的順序 是模糊的，因此系統支援指定 這些欄位預期的順序。將 [DateStyle](https://docs.postgresql.tw/19/server-administration/runtime-config/runtime-config-client.md#GUC-DATESTYLE) 參數 設為 `MDY`，可選擇月-日-年的解讀方式， 設為 `DMY`，可選擇日-月-年的解讀方式， 或設為 `YMD`，可選擇年-月-日的解讀方式。

在處理日期／時間輸入時， PostgreSQL 比 SQL 標準要求的 更為靈活。 關於日期／時間輸入的精確剖析規則， 以及所能識別的文字欄位（包括月份、星期幾與 時區），請參閱[附錄 B](https://docs.postgresql.tw/19/appendixes/datetime-appendix.md)。

請記得，任何日期或時間常值輸入， 都需要像文字字串一樣，用單引號括起來。詳情請參閱 [4.1.2.7 節](https://docs.postgresql.tw/19/the-sql-language/sql-syntax/sql-syntax-lexical/sql-syntax-constants.md#sql-syntax-constants-generic)。 SQL 要求使用以下語法

```

type [ (p) ] 'value'
```

其中 *`p`* 是一個選用的精確度 指定，用來給出秒欄位中的小數 位數。可以為 `time`、`timestamp` 與 `interval` 型別指定精確度， 範圍可以是 0 到 6。 若在常值規格中未指定精確度， 則預設為該常值本身的精確度（但 不超過 6 位）。

### 8.5.1.1. 日期 [#](#datatype-datetime-input-dates) <a href="#datatype-datetime-input-dates" id="datatype-datetime-input-dates"></a>

[表 8.10](#datatype-datetime-date-table) 展示了 `date` 型別的一些可能輸入方式。

### 表 8.10. 日期輸入 [#](#datatype-datetime-date-table) <a href="#datatype-datetime-date-table" id="datatype-datetime-date-table"></a>

| 範例               | 說明                                                                              |
| ---------------- | ------------------------------------------------------------------------------- |
| 1999-01-08       | ISO 8601；任何模式下皆為 1 月 8 日 （建議格式）                                                 |
| January 8, 1999  | 在任何 `datestyle` 輸入模式下皆無歧義                                                       |
| 1/8/1999         | `MDY` 模式下為 1 月 8 日； `DMY` 模式下為 8 月 1 日                                          |
| 1/18/1999        | `MDY` 模式下為 1 月 18 日； 在其他模式下會被拒絕                                                 |
| 01/02/03         | `MDY` 模式下為 2003 年 1 月 2 日； `DMY` 模式下為 2003 年 2 月 1 日； `YMD` 模式下為 2001 年 2 月 3 日 |
| 1999-Jan-08      | 任何模式下皆為 1 月 8 日                                                                 |
| Jan-08-1999      | 任何模式下皆為 1 月 8 日                                                                 |
| 08-Jan-1999      | 任何模式下皆為 1 月 8 日                                                                 |
| 99-Jan-08        | `YMD` 模式下為 1 月 8 日，否則錯誤                                                         |
| 08-Jan-99        | 1 月 8 日，但在 `YMD` 模式下錯誤                                                          |
| Jan-08-99        | 1 月 8 日，但在 `YMD` 模式下錯誤                                                          |
| 19990108         | ISO 8601；任何模式下皆為 1999 年 1 月 8 日                                                 |
| 990108           | ISO 8601；任何模式下皆為 1999 年 1 月 8 日                                                 |
| 1999.008         | 年份與該年的第幾天                                                                       |
| J2451187         | 儒略日（Julian date）                                                                |
| January 8, 99 BC | 西元前 99 年                                                                        |

<br>

### 8.5.1.2. 時刻 [#](#datatype-datetime-input-times) <a href="#datatype-datetime-input-times" id="datatype-datetime-input-times"></a>

時刻型別為 `time [ (p) ] without time zone` 與 `time [ (p) ] with time zone`。單獨寫 `time`，等同於 `time without time zone`。

這些型別的有效輸入，由一個時刻， 加上選用的時區組成。（請參閱[表 8.11](#datatype-datetime-time-table) 與[表 8.12](#datatype-timezone-table)。）若在 `time without time zone` 的輸入中， 指定了時區，該時區會被悄悄地忽略。您也可以指定 日期，但它會被忽略，除非您使用了一個 牽涉到日光節約時間（daylight-savings）規則的時區名稱， 例如 `America/New_York`。在這種情況下， 必須指定日期，才能判斷應該套用標準時間 還是日光節約時間。適當的時區偏移量， 會被記錄在 `time with time zone` 值中，並依原樣輸出； 不會依目前的時區來調整。

### 表 8.11. 時刻輸入 [#](#datatype-datetime-time-table) <a href="#datatype-datetime-time-table" id="datatype-datetime-time-table"></a>

| 範例                                     | 說明                           |
| -------------------------------------- | ---------------------------- |
| `04:05:06.789`                         | ISO 8601                     |
| `04:05:06`                             | ISO 8601                     |
| `04:05`                                | ISO 8601                     |
| `040506`                               | ISO 8601                     |
| `04:05 AM`                             | 與 04:05 相同；AM 不影響數值          |
| `04:05 PM`                             | 與 16:05 相同；輸入的小時必須 <= 12     |
| `04:05:06.789-8`                       | ISO 8601，時區以 UTC 偏移量表示       |
| `04:05:06-08:00`                       | ISO 8601，時區以 UTC 偏移量表示       |
| `04:05-08:00`                          | ISO 8601，時區以 UTC 偏移量表示       |
| `040506-08`                            | ISO 8601，時區以 UTC 偏移量表示       |
| `040506+0730`                          | ISO 8601，時區以帶小數小時的 UTC 偏移量表示 |
| `040506+07:30:00`                      | UTC 偏移量精確到秒（ISO 8601 中不允許）   |
| `04:05:06 PST`                         | 以縮寫指定時區                      |
| `2003-04-12 04:05:06 America/New_York` | 以完整名稱指定時區                    |

<br>

### 表 8.12. 時區輸入 [#](#datatype-timezone-table) <a href="#datatype-timezone-table" id="datatype-timezone-table"></a>

| 範例                 | 說明                              |
| ------------------ | ------------------------------- |
| `PST`              | 縮寫（代表 Pacific Standard Time）    |
| `America/New_York` | 完整時區名稱                          |
| `PST8PDT`          | POSIX 風格的時區規格                   |
| `-8:00:00`         | PST 的 UTC 偏移量                   |
| `-8:00`            | PST 的 UTC 偏移量（ISO 8601 擴充格式）    |
| `-800`             | PST 的 UTC 偏移量（ISO 8601 基本格式）    |
| `-8`               | PST 的 UTC 偏移量（ISO 8601 基本格式）    |
| `zulu`             | UTC 的軍用縮寫                       |
| `z`                | `zulu` 的簡短形式（同樣也出現在 ISO 8601 中） |

<br>

關於如何指定時區的更多資訊，請參閱[8.5.3 節](https://docs.postgresql.tw/19/the-sql-language/datatype/datatype-datetime.md#datatype-timezones)。

### 8.5.1.3. 時間戳記 [#](#datatype-datetime-input-time-stamps) <a href="#datatype-datetime-input-time-stamps" id="datatype-datetime-input-time-stamps"></a>

時間戳記型別的有效輸入， 由日期與時刻串接， 後面加上選用的時區， 再加上選用的 `AD` 或 `BC` 組成。 （另外，`AD`/`BC` 也可以出現在 時區之前，但這不是偏好的順序。） 因此：

```

1999-01-08 04:05:06
```

以及：

```

1999-01-08 04:05:06 -8:00
```

都是有效的值，且遵循 ISO 8601 標準。此外，也支援常見的格式：

```

January 8 04:05:06 1999 PST
```

SQL 標準是透過時刻後面 是否出現「+」或「-」符號及時區偏移量， 來區分 `timestamp without time zone` 與 `timestamp with time zone` 常值的。 因此，依照標準，

```

TIMESTAMP '2004-10-19 10:23:54'
```

是一個 `timestamp without time zone`，而

```

TIMESTAMP '2004-10-19 10:23:54+02'
```

則是一個 `timestamp with time zone`。 PostgreSQL 在判斷型別之前， 永遠不會檢視常值字串的內容，因此 會將上述兩者，都視為 `timestamp without time zone`。 若要確保某個常值，被視為 `timestamp with time zone`，請為它明確指定正確的型別：

```

TIMESTAMP WITH TIME ZONE '2004-10-19 10:23:54+02'
```

對於已判定為 `timestamp without time zone` 的值，PostgreSQL 會悄悄地忽略其中的任何時區指示。 也就是說，最終結果值，是從輸入字串中的 日期／時間欄位得出的，不會因時區而調整。

對於 `timestamp with time zone` 的值， 若輸入字串包含明確的時區， 會使用該時區適當的偏移量， 被轉換為 UTC（[*協調世界時 （Universal Coordinated Time）*](https://docs.postgresql.tw/19/appendixes/glossary.md#glossary-utc)）。 若輸入字串中未指定時區， 則會假設它是採用系統 [TimeZone](https://docs.postgresql.tw/19/server-administration/runtime-config/runtime-config-client.md#GUC-TIMEZONE) 參數所指示的時區， 並使用該 `timezone` 時區的偏移量，轉換為 UTC。 無論哪一種情況，該值都會以 UTC 的形式， 儲存在內部，而原本陳述或假設的時區， 並不會被保留。

當輸出 `timestamp with time zone` 值時，永遠會從 UTC 轉換為 目前的 `timezone` 時區，並以該 時區的當地時間顯示。若要以另一個時區來檢視時刻， 可以變更 `timezone`，或使用 `AT TIME ZONE` 結構 （請參閱[9.9.4 節](https://docs.postgresql.tw/19/the-sql-language/functions/functions-datetime/functions-datetime-zoneconvert.md#functions-datetime-zoneconvert)）。

在 `timestamp without time zone` 與 `timestamp with time zone` 之間轉換時， 一般會假設 `timestamp without time zone` 的值， 應該被視為或給定為 `timezone` 的當地時間。可以使用 `AT TIME ZONE` 為這個轉換，指定不同的時區。

### 8.5.1.4. 特殊值 [#](#datatype-datetime-special-values) <a href="#datatype-datetime-special-values" id="datatype-datetime-special-values"></a>

為了方便起見，PostgreSQL 支援幾種特殊的日期／時間輸入值， 如[表 8.13](#datatype-datetime-special-table)所示。值 `infinity` 與 `-infinity` 在系統內部有特殊的表示方式，會原樣 顯示；但其他值只是單純的標記簡寫， 在讀取時，會被轉換為一般的日期／時間值。 （具體來說，`now` 以及相關的字串， 一經讀取，就會立即被轉換為某個特定的時間值。） 在 SQL 指令中，將以下所有這些值 作為常值使用時，都需要以單引號括起來。

### 表 8.13. 特殊日期／時間輸入 [#](#datatype-datetime-special-table) <a href="#datatype-datetime-special-table" id="datatype-datetime-special-table"></a>

| 輸入字串        | 有效型別                          | 說明                                   |
| ----------- | ----------------------------- | ------------------------------------ |
| `epoch`     | `date`、`timestamp`            | 1970-01-01 00:00:00+00（Unix 系統時間的零點） |
| `infinity`  | `date`、`timestamp`、`interval` | 比所有其他時間戳記都晚                          |
| `-infinity` | `date`、`timestamp`、`interval` | 比所有其他時間戳記都早                          |
| `now`       | `date`、`time`、`timestamp`     | 目前交易的開始時間                            |
| `today`     | `date`、`timestamp`            | 今天的午夜（`00:00`）                       |
| `tomorrow`  | `date`、`timestamp`            | 明天的午夜（`00:00`）                       |
| `yesterday` | `date`、`timestamp`            | 昨天的午夜（`00:00`）                       |
| `allballs`  | `time`                        | 00:00:00.00 UTC                      |

<br>

以下相容於 SQL 的函式， 同樣可用來取得對應資料型別的目前時刻值： `CURRENT_DATE`、`CURRENT_TIME`、 `CURRENT_TIMESTAMP`、`LOCALTIME`、 `LOCALTIMESTAMP`。（請參閱[9.9.5 節](https://docs.postgresql.tw/19/the-sql-language/functions/functions-datetime.md#functions-datetime-current)。）請注意，這些是 SQL 函式，在資料輸入字串中，*不會*被識別。

### 注意 [#](#xiao-xin) <a href="#xiao-xin" id="xiao-xin"></a>

{% hint style="warning" %}
雖然輸入字串 `now`、 `today`、`tomorrow` 與 `yesterday`，很適合用於互動式 SQL 指令中，但當該指令被儲存下來，稍後才執行時， 例如在預備陳述式、檢視表與函式定義中， 它們的行為可能會令人意外。這個字串，可能會被轉換為 某個特定的時間值，而該值在過時許久之後， 仍會持續被使用。在這類情境中，請改用 其中一種 SQL 函式。舉例來說， `CURRENT_DATE + 1` 就比 `'tomorrow'::date` 更安全。
{% endhint %}

[返回 8.5. 日期／時間型別](https://docs.postgresql.tw/19/the-sql-language/datatype/datatype-datetime.md#datatype-datetime)

***

原文：[PostgreSQL 18.6 Documentation](https://www.postgresql.org/docs/18/datatype-datetime.html)（原文版本：18.6；核對日期：2026-09-26）


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.postgresql.tw/19/the-sql-language/datatype/datatype-datetime/datatype-datetime-input.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
