作法:存取預先定義的 UTC 和當地時區物件

TimeZoneInfo 類別提供兩個屬性 (UtcLocal),以授與您的程式碼存取預先定義之時區物件的權限。 本主題討論如何存取這些屬性所傳回的 TimeZoneInfo 物件。

存取國際標準時間 (UTC) TimeZoneInfo 物件

  1. 使用 static (在 Visual Basic 中為 Shared) TimeZoneInfo.Utc 屬性來存取國際標準時間。

  2. 繼續透過 TimeZoneInfo.Utc 屬性來存取國際標準時間,而不是指派由物件變數之屬性傳回的 TimeZoneInfo 物件。

存取當地時區

  1. 使用 static (在 Visual Basic 中為 Shared) TimeZoneInfo.Local 屬性來存取本地系統標準時區。

  2. 繼續透過 TimeZoneInfo.Local 屬性來存取本地時區,而不是指派由物件變數之屬性傳回的 TimeZoneInfo 物件。

範例

下列程式碼會使用 TimeZoneInfo.LocalTimeZoneInfo.Utc 屬性轉換美國及加拿大東部標準時區的時間,以及對主控台顯示時區名稱。

// Create Eastern Standard Time value and TimeZoneInfo object
DateTime estTime = new DateTime(2007, 1, 1, 00, 00, 00);
string timeZoneName = "Eastern Standard Time";
try
{
    TimeZoneInfo est = TimeZoneInfo.FindSystemTimeZoneById(timeZoneName);

    // Convert EST to local time
    DateTime localTime = TimeZoneInfo.ConvertTime(estTime, est, TimeZoneInfo.Local);
    Console.WriteLine("At {0} {1}, the local time is {2} {3}.",
            estTime,
            est,
            localTime,
            TimeZoneInfo.Local.IsDaylightSavingTime(localTime) ?
                      TimeZoneInfo.Local.DaylightName :
                      TimeZoneInfo.Local.StandardName);

    // Convert EST to UTC
    DateTime utcTime = TimeZoneInfo.ConvertTime(estTime, est, TimeZoneInfo.Utc);
    Console.WriteLine("At {0} {1}, the time is {2} {3}.",
            estTime,
            est,
            utcTime,
            TimeZoneInfo.Utc.StandardName);
}
catch (TimeZoneNotFoundException)
{
    Console.WriteLine("The {timeZoneName} zone cannot be found in the registry.");
}
catch (InvalidTimeZoneException)
{
    Console.WriteLine("The registry contains invalid data for the {timeZoneName} zone.");
}

// The example produces the following output to the console:
//    At 1/1/2007 12:00:00 AM (UTC-05:00) Eastern Time (US & Canada), the local time is 1/1/2007 12:00:00 AM Eastern Standard Time.
//    At 1/1/2007 12:00:00 AM (UTC-05:00) Eastern Time (US & Canada), the time is 1/1/2007 5:00:00 AM UTC.

' Create Eastern Standard Time value and TimeZoneInfo object      
Dim estTime As Date = #01/01/2007 00:00:00#
Dim timeZoneName As String = "Eastern Standard Time"
Try
    Dim est As TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(timeZoneName)

    ' Convert EST to local time
    Dim localTime As Date = TimeZoneInfo.ConvertTime(estTime, est, TimeZoneInfo.Local)
    Console.WriteLine("At {0} {1}, the local time is {2} {3}.", _
            estTime, _
            est, _
            localTime, _
            IIf(TimeZoneInfo.Local.IsDaylightSavingTime(localTime), _
                TimeZoneInfo.Local.DaylightName, _
                TimeZoneInfo.Local.StandardName))

    ' Convert EST to UTC
    Dim utcTime As Date = TimeZoneInfo.ConvertTime(estTime, est, TimeZoneInfo.Utc)
    Console.WriteLine("At {0} {1}, the time is {2} {3}.", _
            estTime, _
            est, _
            utcTime, _
            TimeZoneInfo.Utc.StandardName)
Catch e As TimeZoneNotFoundException
    Console.WriteLine("The {0} zone cannot be found in the registry.", _
                      timeZoneName)
Catch e As InvalidTimeZoneException
    Console.WriteLine("The registry contains invalid data for the {0} zone.", _
                      timeZoneName)
End Try

您應該一律透過 TimeZoneInfo.Local 屬性存取本地時區,而不是將本地時區指派給 TimeZoneInfo 物件變數。 同樣地,您應該一律透過 TimeZoneInfo.Utc 屬性存取國際標準時間,而不是將 UTC 時區指派給 TimeZoneInfo 物件變數。 這可防止 TimeZoneInfo.ClearCachedData 呼叫方法使 TimeZoneInfo 物件變數失效。

編譯程式碼

這個範例需要:

  • 透過 using 陳述式匯入 System 命名空間 (C# 程式碼中的必要項目)。

另請參閱