TimeZone.ToLocalTime(DateTime) 方法

定義

傳回對應到指定之日期與時間值的本地時間。Returns the local time that corresponds to a specified date and time value.

public:
 virtual DateTime ToLocalTime(DateTime time);
public virtual DateTime ToLocalTime (DateTime time);
abstract member ToLocalTime : DateTime -> DateTime
override this.ToLocalTime : DateTime -> DateTime
Public Overridable Function ToLocalTime (time As DateTime) As DateTime

參數

time
DateTime

Coordinated Universal Time (UTC) 時間。A Coordinated Universal Time (UTC) time.

傳回

DateTime 物件,其值為對應至 time 的本地時間。A DateTime object whose value is the local time that corresponds to time.

備註

下表顯示time參數與這個方法所傳回的DateTime值之間的關聯性。The following table shows the relationship between the time parameter and the DateTime value returned by this method.

time 參數time parameter 行為Behavior 傳回值Return value
國際標準時間 (UTC) 時間 (DateTimeKind.Utc)。A Coordinated Universal Time (UTC) time (DateTimeKind.Utc). 將時間從 UTC 轉換為當地時間。Converts the time from UTC to the local time. DateTime 物件,其值為對應至 time 的本地時間。A DateTime object whose value is the local time that corresponds to time.
當地時間 (DateTimeKind.Local)。A local time (DateTimeKind.Local). 不需要轉換。No conversion necessary. time參數DateTime表示的相同值。The same DateTime value represented by the time parameter.
未指定的時間DateTimeKind.Unspecified()。An unspecified time (DateTimeKind.Unspecified). 假設時間為 UTC, 並將它從 UTC 轉換為當地時間。Assumes that the time is UTC and converts it from UTC to the local time. DateTime 物件,其值為對應至 time 的本地時間。A DateTime object whose value is the local time that corresponds to time.

如果當地時區觀察日光節約時間, 當執行ToLocalTime轉換時, 會將目前time的調整規則套用至。If the local time zone observes daylight saving time, ToLocalTime applies the current adjustment rule to time when performing the conversion.

注意

ToLocalTime方法只會辨識當地時區的目前日光節約時間調整規則。The ToLocalTime method recognizes only the current daylight saving time adjustment rule for the local time zone. 如此一來, 只有在最新的調整規則生效期間, 才保證能夠精確地傳回與特定 UTC 時間相對應的當地時間。As a result, it is guaranteed to accurately return the local time corresponding to a particular UTC time only during the period in which the latest adjustment rule is in effect. 如果time是以先前的調整規則為依據的歷史日期和時間值, 則可能會傳回不正確的結果。It may return inaccurate results if time is a historic date and time value that was subject to a previous adjustment rule.

方法會對應TimeZoneInfo.ConvertTimeFromUtc至方法, 並將destinationTimeZone其參數設定TimeZoneInfo.Local為。 ToLocalTimeThe ToLocalTime method corresponds to the TimeZoneInfo.ConvertTimeFromUtc method with its destinationTimeZone parameter set to TimeZoneInfo.Local. 如果可能, 請使用TimeZoneInfo.ConvertTimeFromUtc方法。Whenever possible, use the TimeZoneInfo.ConvertTimeFromUtc method.

給繼承者的注意事項

雖然並非必要, 但在大部分的情況下, 以 .NET Framework 版本2.0 執行的衍生類別應該覆寫這個方法的預設實值。Although it is not required, in most cases derived classes running under the .NET Framework version 2.0 should override the default implementation of this method. 在 .NET Framework 版本1.0 和1.1 中, ToLocalTime方法會GetUtcOffset(DateTime)呼叫方法, 並在傳回當地時間時, 針對日光節約時間進行調整。In the .NET Framework versions 1.0 and 1.1, the ToLocalTime method called the GetUtcOffset(DateTime) method and adjusted for daylight saving time when returning the local time. 不過, 從 .NET Framework 2.0 開始, 預設實值的行為取決於Kind time參數的屬性。However, starting with the .NET Framework 2.0, the behavior of the default implementation depends on the Kind property of the time parameter. 如果其值為Local, 則這個time方法會傳回未變更的。If its value is Local, this method returns time unchanged. 如果其值Utc為或Unspecified, 則此方法會time假設為 UTC, 並將其轉換GetUtcOffset(DateTime)為本機系統時間, 而不會呼叫方法。If its value is either Utc or Unspecified, this method assumes time is UTC and converts it to the local system time without calling the GetUtcOffset(DateTime) method.

下列程式碼提供ToLocalTime方法預設執行的簡單覆寫。The following code provides a simple override of the default implementation of the ToLocalTime method. 在此程式碼中internalTimeZone , 變數代表TimeZone類別的私用實例:In this code, the internalTimeZone variable represents a private instance of the TimeZone class: [!code-csharpSystem.TimeZone.ToLocalTime#1] [!code-vbSystem.TimeZone.ToLocalTime#1][!code-csharpSystem.TimeZone.ToLocalTime#1] [!code-vbSystem.TimeZone.ToLocalTime#1]

適用於

另請參閱