日期、時間和時區Dates, times, and time zones

除了基本 DateTime 結構之外,.NET 還會提供下列支援處理時區的類別:In addition to the basic DateTime structure, .NET provides the following classes that support working with time zones:

  • TimeZone

    使用這個類別,以處理系統的當地時區和國際標準時間 (UTC) 區域。Use this class to work with the system's local time zone and the Coordinated Universal Time (UTC) zone. 類別的功能TimeZone大部分是TimeZoneInfo由類別所取代。The functionality of the TimeZone class is largely superseded by the TimeZoneInfo class.

  • TimeZoneInfo

    使用此類別可搭配系統上預先定義的任何時區建立新的時區,以及將一個時區的日期和時間輕鬆轉換到另一個時區。Use this class to work with any time zone that is predefined on a system, to create new time zones, and to easily convert dates and times from one time zone to another. 在開發新程式時,請使用 TimeZoneInfo 類別而非 TimeZone 類別。For new development, use the TimeZoneInfo class instead of the TimeZone class.

  • DateTimeOffset

    使用這個結構,來處理與 UTC 的位移 (或差異) 已知的日期和時間。Use this structure to work with dates and times whose offset (or difference) from UTC is known. DateTimeOffset 結構會合併日期和時間值與該時間與 UTC 的位移。The DateTimeOffset structure combines a date and time value with that time's offset from UTC. 基於其與 UTC 的關聯性,個別日期和時間值會明確地識別單一時間點。Because of its relationship to UTC, an individual date and time value unambiguously identifies a single point in time. 這可讓 DateTimeOffset 值從某部電腦到另一部電腦的可攜性優於 DateTime 值。This makes a DateTimeOffset value more portable from one computer to another than a DateTime value.

文件的這一節提供使用時區以及建立可轉換不同時區之日期和時間的時區感知應用程式所需的資訊。This section of the documentation provides the information that you need to work with time zones and to create time zone-aware applications that can convert dates and times from one time zone to another.

本節內容In this section

時區概觀 - 討論有關建立時區感知應用程式的術語、概念和問題。Time zone overview Discusses the terminology, concepts, and issues involved in creating time zone-aware applications.

在 DateTime、DateTimeOffset、TimeSpan 與 TimeZoneInfo 之間選擇 - 討論在處理日期和時間資料時何時使用 DateTimeDateTimeOffsetTimeZoneInfo 類型。Choosing between DateTime, DateTimeOffset, TimeSpan, and TimeZoneInfo Discusses when to use the DateTime, DateTimeOffset, and TimeZoneInfo types when working with date and time data.

尋找定義於本機系統的時區 - 描述如何列舉在本機系統上找到的時區。Finding the time zones defined on a local system Describes how to enumerate the time zones found on a local system.

如何:列舉電腦上的時區提供範例,以列舉電腦登錄中所定義的時區,並讓使用者從清單中選取預先定義的時區。How to: Enumerate time zones present on a computer Provides examples that enumerate the time zones defined in a computer's registry and that let users select a predefined time zone from a list.

如何:存取預先定義的 UTC 和當地時區物件說明如何存取國際標準時間和當地時區。How to: Access the predefined UTC and local time zone objects Describes how to access Coordinated Universal Time and the local time zone.

如何:具現化 TimeZoneInfo物件說明如何從本機TimeZoneInfo系統登錄具現化物件。How to: Instantiate a TimeZoneInfo object Describes how to instantiate a TimeZoneInfo object from the local system registry.

具現化 DateTimeOffset 物件 - 討論可以具現化 DateTimeOffset 物件的方式,以及 DateTime 值可以轉換為 DateTimeOffset 值的方式。Instantiating a DateTimeOffset object Discusses the ways in which a DateTimeOffset object can be instantiated, and the ways in which a DateTime value can be converted to a DateTimeOffset value.

如何:建立沒有調整規則的時區描述如何建立不支援日光節約時間轉換的自訂時區。How to: Create time zones without adjustment rules Describes how to create a custom time zone that does not support the transition to and from daylight saving time.

如何:建立具有調整規則的時區描述如何建立支援一或多個日光節約時間轉換的自訂時區。How to: Create time zones with adjustment rules Describes how to create a custom time zone that supports one or more transitions to and from daylight saving time.

儲存和還原時區 - 描述 TimeZoneInfo 對時區資料之序列化和還原序列化的支援,並說明可使用這些功能的一些案例。Saving and restoring time zones Describes TimeZoneInfo support for serialization and deserialization of time zone data and illustrates some of the scenarios in which these features can be used.

如何:將時區儲存到內嵌資源說明如何建立自訂時區,並將其資訊儲存在資源檔中。How to: Save time zones to an embedded resource Describes how to create a custom time zone and save its information in a resource file.

如何:從內嵌資源還原時區說明如何具現化已儲存至內嵌資源檔的自訂時區。How to: Restore time zones from an embedded resource Describes how to instantiate custom time zones that have been saved to an embedded resource file.

使用日期和時間執行算術運算 - 討論如何加上、減去和比較 DateTimeDateTimeOffset 值的相關問題。Performing arithmetic operations with dates and times Discusses the issues involved in adding, subtracting, and comparing DateTime and DateTimeOffset values.

如何:使用日期和時間運算中的時區討論如何執行反映時區調整規則的日期和時間運算。How to: Use time zones in date and time arithmetic Discusses how to perform date and time arithmetic that reflects a time zone's adjustment rules.

在 DateTime 和 DateTimeOffset 之間轉換 - 描述如何在 DateTimeDateTimeOffset 值之間轉換。Converting between DateTime and DateTimeOffset Describes how to convert between DateTime and DateTimeOffset values.

在各時區間轉換時間 - 描述如何將時間從某個時區轉換為另一個時區。Converting times between time zones Describes how to convert times from one time zone to another.

如何:解決不明確的時間描述如何藉由將不明確的時間對應至時區的標準時間來加以解決。How to: Resolve ambiguous times Describes how to resolve an ambiguous time by mapping it to the time zone's standard time.

如何:讓使用者解決不明確的時間說明如何讓使用者判斷不明確當地時間與國際標準時間之間的對應。How to: Let users resolve ambiguous times Describes how to let a user determine the mapping between an ambiguous local time and Coordinated Universal Time.

參考資料Reference

System.TimeZoneInfo