如何:取得 TimeZoneInfo 物件
取得 TimeZoneInfo 物件最常見的方法是從登錄擷取其相關信息。 若要取得 物件,請在登錄中呼叫 static
(Shared
在 Visual Basic 中為 ) TimeZoneInfo.FindSystemTimeZoneById 方法。 處理 方法擲回的任何例外狀況,特別是 TimeZoneNotFoundException 登錄中未定義時區時所擲回的 。
注意
從 .NET 8 開始,會傳回快取TimeZoneInfo的物件,TimeZoneInfo.FindSystemTimeZoneById而不是具現化新的物件。 如需詳細資訊,請參閱 FindSystemTimeZoneById 不會傳回新的物件。
範例
下列程式碼會擷取 TimeZoneInfo 物件,其代表美加東部標準時間區域,並顯示與本地時間對應的美加東部標準時間。
DateTime timeNow = DateTime.Now;
try
{
TimeZoneInfo easternZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
DateTime easternTimeNow = TimeZoneInfo.ConvertTime(
timeNow,
TimeZoneInfo.Local,
easternZone
);
Console.WriteLine("{0} {1} corresponds to {2} {3}.",
timeNow,
TimeZoneInfo.Local.IsDaylightSavingTime(timeNow) ?
TimeZoneInfo.Local.DaylightName :
TimeZoneInfo.Local.StandardName,
easternTimeNow,
easternZone.IsDaylightSavingTime(easternTimeNow) ?
easternZone.DaylightName :
easternZone.StandardName);
}
// Handle exception
//
// As an alternative to simply displaying an error message, an alternate Eastern
// Standard Time TimeZoneInfo object could be instantiated here either by restoring
// it from a serialized string or by providing the necessary data to the
// CreateCustomTimeZone method.
catch (TimeZoneNotFoundException)
{
Console.WriteLine("The Eastern Standard Time Zone cannot be found on the local system.");
}
catch (InvalidTimeZoneException)
{
Console.WriteLine("The Eastern Standard Time Zone contains invalid or missing data.");
}
catch (SecurityException)
{
Console.WriteLine("The application lacks permission to read time zone information from the registry.");
}
catch (OutOfMemoryException)
{
Console.WriteLine("Not enough memory is available to load information on the Eastern Standard Time zone.");
}
// If we weren't passing FindSystemTimeZoneById a literal string, we also
// would handle an ArgumentNullException.
Dim timeNow As Date = Date.Now
Try
Dim easternZone As TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time")
Dim easternTimeNow As Date = TimeZoneInfo.ConvertTime(timeNow, TimeZoneInfo.Local, easternZone)
Console.WriteLine("{0} {1} corresponds to {2} {3}.", _
timeNow, _
IIf(TimeZoneInfo.Local.IsDaylightSavingTime(timeNow), _
TimeZoneInfo.Local.DaylightName, TimeZoneInfo.Local.StandardName), _
easternTimeNow, _
IIf(easternZone.IsDaylightSavingTime(easternTimeNow), _
easternZone.DaylightName, easternZone.StandardName))
' Handle exception
'
' As an alternative to simply displaying an error message, an alternate Eastern
' Standard Time TimeZoneInfo object could be instantiated here either by restoring
' it from a serialized string or by providing the necessary data to the
' CreateCustomTimeZone method.
Catch e As TimeZoneNotFoundException
Console.WriteLine("The Eastern Standard Time Zone cannot be found on the local system.")
Catch e As InvalidTimeZoneException
Console.WriteLine("The Eastern Standard Time Zone contains invalid or missing data.")
Catch e As SecurityException
Console.WriteLine("The application lacks permission to read time zone information from the registry.")
Catch e As OutOfMemoryException
Console.WriteLine("Not enough memory is available to load information on the Eastern Standard Time zone.")
' If we weren't passing FindSystemTimeZoneById a literal string, we also
' would handle an ArgumentNullException.
End Try
TimeZoneInfo.FindSystemTimeZoneById 方法的單一參數是您想要擷取之時區的識別項,其對應至物件的 TimeZoneInfo.Id 屬性。 時區識別項是唯一識別時區的索引鍵欄位。 雖然大部分的索引鍵相對較短,但時區識別項相較之下就很長。 在大部分情況下,其值會對應到 StandardName 物件的 TimeZoneInfo 屬性,其用來提供時區標準時間的名稱。 不過仍有例外狀況。 若要確定您提供了有效的識別項,最好的方法是列舉系統上可用的時區,並記下存在於其之上的時區識別項。 如需圖例,請參閱 How to: Enumerate time zones present on a computer。 尋找 本機系統 發行項上定義的時區也包含所選時區標識碼的清單。
如果找到時區,此方法會傳回其 TimeZoneInfo 物件。 如果找不到時區,方法會擲回 TimeZoneNotFoundException。 如果找到時區,但其資料已損毀或不完整,則方法會擲回 InvalidTimeZoneException。
如果您的應用程式依賴於必須存在的時區,則您應該先呼叫 FindSystemTimeZoneById 方法,以便從登錄擷取時區資訊。 如果方法呼叫失敗,則例外狀況處理常式應建立新的時區執行個體,或透過將序列化 TimeZoneInfo 物件還原序列化,以重新建立時區執行個體。 如需範例,請參閱如何:從內嵌資源還原時區。