日期、时间和时区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 之间进行选择 讨论在 DateTime DateTimeOffset TimeZoneInfo 处理日期和时间数据时何时使用、和类型。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