方法: 定義済みの UTC オブジェクトおよびローカル タイム ゾーン オブジェクトにアクセスする

TimeZoneInfo クラスに用意されている Utc および Local という 2 つのプロパティを使用すると、コードから定義済みのタイム ゾーン オブジェクトにアクセスできます。 このトピックでは、これらのプロパティから返される TimeZoneInfo オブジェクトにアクセスする方法について説明します。

世界協定時刻 (UTC) の TimeZoneInfo オブジェクトにアクセスするには

  1. static (Visual Basic では Shared) TimeZoneInfo.Utc プロパティを使用して、協定世界時にアクセスします。

  2. プロパティから返された TimeZoneInfo オブジェクトをオブジェクト変数に割り当てるのではなく、そのまま TimeZoneInfo.Utc プロパティを使用して協定世界時にアクセスします。

ローカル タイム ゾーンにアクセスするには

  1. static (Visual Basic では Shared) TimeZoneInfo.Local プロパティを使用して、ローカル システム タイム ゾーンにアクセスします。

  2. プロパティから返された TimeZoneInfo オブジェクトをオブジェクト変数に割り当てるのではなく、そのまま TimeZoneInfo.Local プロパティを使用してローカル タイム ゾーンにアクセスします。

次のコードでは、TimeZoneInfo.Local および TimeZoneInfo.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 オブジェクト変数に割り当てることはせず、常に TimeZoneInfo.Local プロパティをお使いください。 同様に、協定世界時にアクセスする場合は、UTC ゾーンを TimeZoneInfo オブジェクト変数に割り当てることはせず、常に TimeZoneInfo.Utc プロパティをお使いください。 これにより、TimeZoneInfo メソッド呼び出しによって TimeZoneInfo.ClearCachedData オブジェクト変数が無効になるのを防ぐことができます。

コードのコンパイル

この例で必要な要素は次のとおりです。

  • using ステートメント (C# コードでは必須) を使用して System 名前空間がインポートされること。

関連項目