DateTime と DateTimeOffset 間の変換

DateTimeOffset 構造体では、DateTime 構造体よりも高度なタイム ゾーン対応を実現しますが、メソッド呼び出しには DateTime パラメーターがよく使用されます。 この方法のため、DateTimeOffset 値を DateTime 値に、またはその逆方向に変換できることが重要です。 この記事では、タイム ゾーン情報をできるだけ多く保持するようにこれらの変換を実行する方法を示します。

注意

DateTime 型と DateTimeOffset 型には両方とも、タイム ゾーンの時刻を表す際に制限があります。 DateTime では、その Kind プロパティによって世界協定時刻 (UTC) とシステムのローカル タイム ゾーンのみを反映できます。 DateTimeOffset では、UTC からの時間のオフセットを反映しますが、そのオフセットが所属する実際のタイム ゾーンは反映しません。 時刻の値とタイム ゾーンのサポートの詳細については、「DateTime DateTimeOffset TimeSpan、および TimeZoneInfo の使い分け」を参照してください。

DateTime から DateTimeOffset への変換

DateTimeOffset 構造体には、ほとんどの変換に適した、DateTime から DateTimeOffset への変換を実行する次の 2 つの同等の方法が用意されています。

UTC と現地のDateTime 値について、変換後の DateTimeOffset 値の Offset プロパティには、UTC またはローカル タイム ゾーン オフセットが正確に反映されます。 たとえば、次のコードによって、UTC 時刻が同等の DateTimeOffset 値に変換されます。

DateTime utcTime1 = new DateTime(2008, 6, 19, 7, 0, 0);
utcTime1 = DateTime.SpecifyKind(utcTime1, DateTimeKind.Utc);
DateTimeOffset utcTime2 = utcTime1;
Console.WriteLine("Converted {0} {1} to a DateTimeOffset value of {2}",
                  utcTime1,
                  utcTime1.Kind,
                  utcTime2);
// This example displays the following output to the console:
//    Converted 6/19/2008 7:00:00 AM Utc to a DateTimeOffset value of 6/19/2008 7:00:00 AM +00:00
Dim utcTime1 As Date = Date.SpecifyKind(#06/19/2008 7:00AM#, _
                                        DateTimeKind.Utc)
Dim utcTime2 As DateTimeOffset = utcTime1
Console.WriteLine("Converted {0} {1} to a DateTimeOffset value of {2}", _
                  utcTime1, _
                  utcTime1.Kind.ToString(), _
                  utcTime2)
' This example displays the following output to the console:
'    Converted 6/19/2008 7:00:00 AM Utc to a DateTimeOffset value of 6/19/2008 7:00:00 AM +00:00                        

この場合、utcTime2 変数のオフセットは 00:00 です。 同様に、次のコードによって、現地時刻が同等の DateTimeOffset 値に変換されます。

DateTime localTime1 = new DateTime(2008, 6, 19, 7, 0, 0);
localTime1 = DateTime.SpecifyKind(localTime1, DateTimeKind.Local);
DateTimeOffset localTime2 = localTime1;
Console.WriteLine("Converted {0} {1} to a DateTimeOffset value of {2}",
                  localTime1,
                  localTime1.Kind,
                  localTime2);
// This example displays the following output to the console:
//    Converted 6/19/2008 7:00:00 AM Local to a DateTimeOffset value of 6/19/2008 7:00:00 AM -07:00
Dim localTime1 As Date = Date.SpecifyKind(#06/19/2008 7:00AM#, DateTimeKind.Local)
Dim localTime2 As DateTimeOffset = localTime1
Console.WriteLine("Converted {0} {1} to a DateTimeOffset value of {2}", _
                  localTime1, _
                  localTime1.Kind.ToString(), _
                  localTime2)
' This example displays the following output to the console:
'    Converted 6/19/2008 7:00:00 AM Local to a DateTimeOffset value of 6/19/2008 7:00:00 AM -07:00

ただし、Kind プロパティが DateTimeKind.Unspecified である DateTime 値については、これらの 2 つの変換メソッドによって、オフセットがローカル タイム ゾーンのオフセットである DateTimeOffset 値が生成されます。 この変換を次の例に示します。これは、米国の太平洋標準時ゾーンで実行されます。

DateTime time1 = new DateTime(2008, 6, 19, 7, 0, 0);  // Kind is DateTimeKind.Unspecified
DateTimeOffset time2 = time1;
Console.WriteLine("Converted {0} {1} to a DateTimeOffset value of {2}",
                  time1,
                  time1.Kind,
                  time2);
// This example displays the following output to the console:
//    Converted 6/19/2008 7:00:00 AM Unspecified to a DateTimeOffset value of 6/19/2008 7:00:00 AM -07:00
Dim time1 As Date = #06/19/2008 7:00AM#      ' Kind is DateTimeKind.Unspecified
Dim time2 As DateTimeOffset = time1
Console.WriteLine("Converted {0} {1} to a DateTimeOffset value of {2}", _
                  time1, _
                  time1.Kind.ToString(), _
                  time2)
' This example displays the following output to the console:
'    Converted 6/19/2008 7:00:00 AM Unspecified to a DateTimeOffset value of 6/19/2008 7:00:00 AM -07:00

DateTime 値がローカル タイム ゾーンまたは UTC 以外の何らかの日付と時刻を反映している場合は、オーバーロードされた DateTimeOffset コンストラクターを呼び出して、この値を DateTimeOffset 値に変換し、そのタイム ゾーン情報を保持することができます。 たとえば、次の例では、中部標準時を反映する DateTimeOffset オブジェクトをインスタンス化します。

DateTime time1 = new DateTime(2008, 6, 19, 7, 0, 0);     // Kind is DateTimeKind.Unspecified
try
{
   DateTimeOffset time2 = new DateTimeOffset(time1,
                  TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time").GetUtcOffset(time1));
   Console.WriteLine("Converted {0} {1} to a DateTime value of {2}",
                     time1,
                     time1.Kind,
                     time2);
}
// Handle exception if time zone is not defined in registry
catch (TimeZoneNotFoundException)
{
   Console.WriteLine("Unable to identify target time zone for conversion.");
}
// This example displays the following output to the console:
//    Converted 6/19/2008 7:00:00 AM Unspecified to a DateTime value of 6/19/2008 7:00:00 AM -05:00
Dim time1 As Date = #06/19/2008 7:00AM#      ' Kind is DateTimeKind.Unspecified
Try
    Dim time2 As New DateTimeOffset(time1, _
                     TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time").GetUtcOffset(time1))
    Console.WriteLine("Converted {0} {1} to a DateTime value of {2}", _
                      time1, _
                      time1.Kind.ToString(), _
                      time2)
    ' Handle exception if time zone is not defined in registry
Catch e As TimeZoneNotFoundException
    Console.WriteLine("Unable to identify target time zone for conversion.")
End Try
' This example displays the following output to the console:
'    Converted 6/19/2008 7:00:00 AM Unspecified to a DateTime value of 6/19/2008 7:00:00 AM -05:00

このコンストラクター オーバーロードの 2 番目のパラメーターは、UTC からの時刻のオフセットを表す TimeSpan オブジェクトです。 その時刻の対応するタイム ゾーンの TimeZoneInfo.GetUtcOffset(DateTime) メソッドを呼び出すことで取得します。 このメソッドの単一のパラメーターは、変換する日付と時刻を表す DateTime 値です。 タイム ゾーンで夏時間がサポートされている場合、このパラメーターにより、このメソッドはその特定の日時に対して適切なオフセットを決定できます。

DateTimeOffset から DateTime への変換

DateTimeOffset から DateTime への変換の実行には、DateTime プロパティが最もよく使用されます。 ただし、次の例に示すように、これによって Kind プロパティが Unspecified である DateTime 値が返されます。

DateTime baseTime = new DateTime(2008, 6, 19, 7, 0, 0);
DateTimeOffset sourceTime;
DateTime targetTime;

// Convert UTC to DateTime value
sourceTime = new DateTimeOffset(baseTime, TimeSpan.Zero);
targetTime = sourceTime.DateTime;
Console.WriteLine("{0} converts to {1} {2}",
                  sourceTime,
                  targetTime,
                  targetTime.Kind);

// Convert local time to DateTime value
sourceTime = new DateTimeOffset(baseTime,
                                TimeZoneInfo.Local.GetUtcOffset(baseTime));
targetTime = sourceTime.DateTime;
Console.WriteLine("{0} converts to {1} {2}",
                  sourceTime,
                  targetTime,
                  targetTime.Kind);

// Convert Central Standard Time to a DateTime value
try
{
   TimeSpan offset = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time").GetUtcOffset(baseTime);
   sourceTime = new DateTimeOffset(baseTime, offset);
   targetTime = sourceTime.DateTime;
   Console.WriteLine("{0} converts to {1} {2}",
                     sourceTime,
                     targetTime,
                     targetTime.Kind);
}
catch (TimeZoneNotFoundException)
{
   Console.WriteLine("Unable to create DateTimeOffset based on U.S. Central Standard Time.");
}
// This example displays the following output to the console:
//    6/19/2008 7:00:00 AM +00:00 converts to 6/19/2008 7:00:00 AM Unspecified
//    6/19/2008 7:00:00 AM -07:00 converts to 6/19/2008 7:00:00 AM Unspecified
//    6/19/2008 7:00:00 AM -05:00 converts to 6/19/2008 7:00:00 AM Unspecified
Const baseTime As Date = #06/19/2008 7:00AM#
Dim sourceTime As DateTimeOffset
Dim targetTime As Date

' Convert UTC to DateTime value
sourceTime = New DateTimeOffset(baseTime, TimeSpan.Zero)
targetTime = sourceTime.DateTime
Console.WriteLine("{0} converts to {1} {2}", _
                  sourceTime, _
                  targetTime, _
                  targetTime.Kind.ToString())

' Convert local time to DateTime value
sourceTime = New DateTimeOffset(baseTime, _
                                TimeZoneInfo.Local.GetUtcOffset(baseTime))
targetTime = sourceTime.DateTime
Console.WriteLine("{0} converts to {1} {2}", _
                  sourceTime, _
                  targetTime, _
                  targetTime.Kind.ToString())

' Convert Central Standard Time to a DateTime value
Try
    Dim offset As TimeSpan = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time").GetUtcOffset(baseTime)
    sourceTime = New DateTimeOffset(baseTime, offset)
    targetTime = sourceTime.DateTime
    Console.WriteLine("{0} converts to {1} {2}", _
                      sourceTime, _
                      targetTime, _
                      targetTime.Kind.ToString())
Catch e As TimeZoneNotFoundException
    Console.WriteLine("Unable to create DateTimeOffset based on U.S. Central Standard Time.")
End Try
' This example displays the following output to the console:
'    6/19/2008 7:00:00 AM +00:00 converts to 6/19/2008 7:00:00 AM Unspecified
'    6/19/2008 7:00:00 AM -07:00 converts to 6/19/2008 7:00:00 AM Unspecified
'    6/19/2008 7:00:00 AM -05:00 converts to 6/19/2008 7:00:00 AM Unspecified                       

前の例では、DateTime プロパティを使用すると、DateTimeOffset 値と UTC との関係に関するすべての情報が変換によって失われることを示しています。 この動作は、UTC 時刻またはシステムの現地時刻に対応する DateTimeOffset 値にも影響を与えます。これは、DateTime 構造体には、その Kind プロパティによってこれらの 2 つのタイム ゾーンのみが反映されるためです。

DateTimeOffsetDateTime 値に変換するときにできるだけ多くのタイム ゾーン情報を保持するには、DateTimeOffset.UtcDateTime プロパティと DateTimeOffset.LocalDateTime プロパティを使用します。

UTC 時刻の変換

変換された DateTime 値が UTC 時刻であることを示すには、DateTimeOffset.UtcDateTime プロパティの値を取得します。 これは、2 つの点で DateTime プロパティとは異なります。

  • Kind プロパティが Utc である DateTime が返される。

  • Offset プロパティ値が TimeSpan.Zero と等しくない場合は、時刻が UTC に変換される。

注意

アプリケーションで、変換された DateTime 値によってある時点が明確に識別される必要がある場合は、DateTimeOffset から DateTime へのすべての変換を処理するために、DateTimeOffset.UtcDateTime プロパティの使用を検討してください。

次のコードでは、UtcDateTime プロパティを使用して、オフセットが TimeSpan.Zero と等しい DateTimeOffset 値を DateTime 値に変換します。

DateTimeOffset utcTime1 = new DateTimeOffset(2008, 6, 19, 7, 0, 0, TimeSpan.Zero);
DateTime utcTime2 = utcTime1.UtcDateTime;
Console.WriteLine("{0} converted to {1} {2}",
                  utcTime1,
                  utcTime2,
                  utcTime2.Kind);
// The example displays the following output to the console:
//   6/19/2008 7:00:00 AM +00:00 converted to 6/19/2008 7:00:00 AM Utc
Dim utcTime1 As New DateTimeOffset(#06/19/2008 7:00AM#, TimeSpan.Zero)
Dim utcTime2 As Date = utcTime1.UtcDateTime
Console.WriteLine("{0} converted to {1} {2}", _
                  utcTime1, _
                  utcTime2, _
                  utcTime2.Kind.ToString())
' The example displays the following output to the console:
'   6/19/2008 7:00:00 AM +00:00 converted to 6/19/2008 7:00:00 AM Utc                              

次のコードでは、UtcDateTime プロパティを使用して、DateTimeOffset 値に対してタイム ゾーン変換と型変換の両方を実行します。

DateTimeOffset originalTime = new DateTimeOffset(2008, 6, 19, 7, 0, 0, new TimeSpan(5, 0, 0));
DateTime utcTime = originalTime.UtcDateTime;
Console.WriteLine("{0} converted to {1} {2}",
                  originalTime,
                  utcTime,
                  utcTime.Kind);
// The example displays the following output to the console:
//       6/19/2008 7:00:00 AM +05:00 converted to 6/19/2008 2:00:00 AM Utc
Dim originalTime As New DateTimeOffset(#6/19/2008 7:00AM#, _
                                       New TimeSpan(5, 0, 0))
Dim utcTime As Date = originalTime.UtcDateTime
Console.WriteLine("{0} converted to {1} {2}", _
                  originalTime, _
                  utcTime, _
                  utcTime.Kind.ToString())
' The example displays the following output to the console:
'       6/19/2008 7:00:00 AM +05:00 converted to 6/19/2008 2:00:00 AM Utc

現地時刻の変換

DateTimeOffset 値が現地時刻を表すことを示すには、DateTimeOffset.DateTime プロパティによって返される DateTime 値を static (Visual Basic では Shared) SpecifyKind メソッドに渡します。 このメソッドによって、その最初のパラメーターとして渡された日付と時刻が返されますが、Kind プロパティが、その 2 番目のパラメーターによって指定された値に設定されます。 次のコードでは、オフセットがローカル タイム ゾーンのオフセットに対応する DateTimeOffset 値を変換する際に、SpecifyKind メソッドを使用します。

DateTime sourceDate = new DateTime(2008, 6, 19, 7, 0, 0);
DateTimeOffset utcTime1 = new DateTimeOffset(sourceDate,
                          TimeZoneInfo.Local.GetUtcOffset(sourceDate));
DateTime utcTime2 = utcTime1.DateTime;
if (utcTime1.Offset.Equals(TimeZoneInfo.Local.GetUtcOffset(utcTime1.DateTime)))
   utcTime2 = DateTime.SpecifyKind(utcTime2, DateTimeKind.Local);

Console.WriteLine("{0} converted to {1} {2}",
                  utcTime1,
                  utcTime2,
                  utcTime2.Kind);
// The example displays the following output to the console:
//   6/19/2008 7:00:00 AM -07:00 converted to 6/19/2008 7:00:00 AM Local
Dim sourceDate As Date = #06/19/2008 7:00AM#
Dim utcTime1 As New DateTimeOffset(sourceDate, _
                                   TimeZoneInfo.Local.GetUtcOffset(sourceDate))
Dim utcTime2 As Date = utcTime1.DateTime
If utcTime1.Offset.Equals(TimeZoneInfo.Local.GetUtcOffset(utcTime1.DateTime)) Then
    utcTime2 = DateTime.SpecifyKind(utcTime2, DateTimeKind.Local)
End If
Console.WriteLine("{0} converted to {1} {2}", _
                  utcTime1, _
                  utcTime2, _
                  utcTime2.Kind.ToString())
' The example displays the following output to the console:
'   6/19/2008 7:00:00 AM -07:00 converted to 6/19/2008 7:00:00 AM Local      

DateTimeOffset.LocalDateTime プロパティを使用して、DateTimeOffset 値をローカルの DateTime 値に変換することもできます。 返される DateTime 値の Kind プロパティは、Local です。 次のコードでは、オフセットがローカル タイム ゾーンのオフセットに対応する DateTimeOffset 値を変換する際に、DateTimeOffset.LocalDateTime プロパティを使用します。

DateTime sourceDate = new DateTime(2008, 6, 19, 7, 0, 0);
DateTimeOffset localTime1 = new DateTimeOffset(sourceDate,
                          TimeZoneInfo.Local.GetUtcOffset(sourceDate));
DateTime localTime2 = localTime1.LocalDateTime;

Console.WriteLine("{0} converted to {1} {2}",
                  localTime1,
                  localTime2,
                  localTime2.Kind);
// The example displays the following output to the console:
//   6/19/2008 7:00:00 AM -07:00 converted to 6/19/2008 7:00:00 AM Local
Dim sourceDate As Date = #06/19/2008 7:00AM#
Dim localTime1 As New DateTimeOffset(sourceDate, _
                                   TimeZoneInfo.Local.GetUtcOffset(sourceDate))
Dim localTime2 As Date = localTime1.LocalDateTime
Console.WriteLine("{0} converted to {1} {2}", _
                  localTime1, _
                  localTime2, _
                  localTime2.Kind.ToString())
' The example displays the following output to the console:
'   6/19/2008 7:00:00 AM -07:00 converted to 6/19/2008 7:00:00 AM Local      

DateTimeOffset.LocalDateTime プロパティを使用して DateTime 値を取得する場合、このプロパティの get アクセサーでは、最初に DateTimeOffset 値を UTC に変換してから、ToLocalTime メソッドを呼び出して現地時刻に変換します。 この動作は、DateTimeOffset.LocalDateTime プロパティから値を取得して、型変換を実行するのと同時にタイム ゾーン変換が実行できることを意味します。 また、変換の実行にローカル タイム ゾーンの調整規則が適用されることも意味します。 次のコードは、DateTimeOffset.LocalDateTime プロパティを使用して型変換とタイム ゾーン変換の両方を実行する例を示しています。 このサンプル出力は、太平洋標準時ゾーン (米国およびカナダ) に設定されているマシン用です。 11 月の日付は太平洋標準時 (UTC-8) で、6 月の日付は夏時間 (UTC-7) です。

DateTimeOffset originalDate;
DateTime localDate;

// Convert time originating in a different time zone
originalDate = new DateTimeOffset(2008, 6, 18, 7, 0, 0,
                                  new TimeSpan(-5, 0, 0));
localDate = originalDate.LocalDateTime;
Console.WriteLine("{0} converted to {1} {2}",
                  originalDate,
                  localDate,
                  localDate.Kind);
// Convert time originating in a different time zone
// so local time zone's adjustment rules are applied
originalDate = new DateTimeOffset(2007, 11, 4, 4, 0, 0,
                                  new TimeSpan(-5, 0, 0));
localDate = originalDate.LocalDateTime;
Console.WriteLine("{0} converted to {1} {2}",
                  originalDate,
                  localDate,
                  localDate.Kind);
// The example displays the following output to the console,
// when you run it on a machine that is set to Pacific Time (US & Canada):
//       6/18/2008 7:00:00 AM -05:00 converted to 6/18/2008 5:00:00 AM Local
//       11/4/2007 4:00:00 AM -05:00 converted to 11/4/2007 1:00:00 AM Local
Dim originalDate As DateTimeOffset
Dim localDate As Date

' Convert time originating in a different time zone
originalDate = New DateTimeOffset(#06/19/2008 7:00AM#, _
                                  New TimeSpan(-5, 0, 0))
localDate = originalDate.LocalDateTime
Console.WriteLine("{0} converted to {1} {2}", _
                  originalDate, _
                  localDate, _
                  localDate.Kind.ToString())
' Convert time originating in a different time zone 
' so local time zone's adjustment rules are applied
originalDate = New DateTimeOffset(#11/04/2007 4:00AM#, _
                                  New TimeSpan(-5, 0, 0))
localDate = originalDate.LocalDateTime
Console.WriteLine("{0} converted to {1} {2}", _
                  originalDate, _
                  localDate, _
                  localDate.Kind.ToString())
' The example displays the following output to the console,
' when you run it on a machine that is set to Pacific Time (US & Canada):
'       6/18/2008 7:00:00 AM -05:00 converted to 6/18/2008 5:00:00 AM Local
'       11/4/2007 4:00:00 AM -05:00 converted to 11/4/2007 1:00:00 AM Local

汎用変換メソッド

次の例では、DateTimeOffset 値を DateTime 値に変換する ConvertFromDateTimeOffset という名前のメソッドを定義します。 オフセットに基づき、DateTimeOffset 値が UTC 時刻、現地時刻、またはその他の時刻であるかどうかが判断され、その結果に応じて、返される日時値の Kind プロパティが定義されます。

static DateTime ConvertFromDateTimeOffset(DateTimeOffset dateTime)
{
   if (dateTime.Offset.Equals(TimeSpan.Zero))
      return dateTime.UtcDateTime;
   else if (dateTime.Offset.Equals(TimeZoneInfo.Local.GetUtcOffset(dateTime.DateTime)))
      return DateTime.SpecifyKind(dateTime.DateTime, DateTimeKind.Local);
   else
      return dateTime.DateTime;
}
Function ConvertFromDateTimeOffset(dateTime As DateTimeOffset) As Date
    If dateTime.Offset.Equals(TimeSpan.Zero) Then
        Return dateTime.UtcDateTime
    ElseIf dateTime.Offset.Equals(TimeZoneInfo.Local.GetUtcOffset(dateTime.DateTime))
        Return Date.SpecifyKind(dateTime.DateTime, DateTimeKind.Local)
    Else
        Return dateTime.DateTime
    End If
End Function

次の例では、ConvertFromDateTimeOffset メソッドを呼び出して、UTC 時刻、現地時刻、および米国の中部標準時ゾーンの時刻を表す DateTimeOffset 値を変換します。

DateTime timeComponent = new DateTime(2008, 6, 19, 7, 0, 0);
DateTime returnedDate;

// Convert UTC time
DateTimeOffset utcTime = new DateTimeOffset(timeComponent, TimeSpan.Zero);
returnedDate = ConvertFromDateTimeOffset(utcTime);
Console.WriteLine("{0} converted to {1} {2}",
                  utcTime,
                  returnedDate,
                  returnedDate.Kind);

// Convert local time
DateTimeOffset localTime = new DateTimeOffset(timeComponent,
                           TimeZoneInfo.Local.GetUtcOffset(timeComponent));
returnedDate = ConvertFromDateTimeOffset(localTime);
Console.WriteLine("{0} converted to {1} {2}",
                  localTime,
                  returnedDate,
                  returnedDate.Kind);

// Convert Central Standard Time
DateTimeOffset cstTime = new DateTimeOffset(timeComponent,
               TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time").GetUtcOffset(timeComponent));
returnedDate = ConvertFromDateTimeOffset(cstTime);
Console.WriteLine("{0} converted to {1} {2}",
                  cstTime,
                  returnedDate,
                  returnedDate.Kind);
// The example displays the following output to the console:
//    6/19/2008 7:00:00 AM +00:00 converted to 6/19/2008 7:00:00 AM Utc
//    6/19/2008 7:00:00 AM -07:00 converted to 6/19/2008 7:00:00 AM Local
//    6/19/2008 7:00:00 AM -05:00 converted to 6/19/2008 7:00:00 AM Unspecified
Dim timeComponent As Date = #06/19/2008 7:00AM#
Dim returnedDate As Date

' Convert UTC time
Dim utcTime As New DateTimeOffset(timeComponent, TimeSpan.Zero)
returnedDate = ConvertFromDateTimeOffset(utcTime)
Console.WriteLine("{0} converted to {1} {2}", _
                  utcTime, _
                  returnedDate, _
                  returnedDate.Kind.ToString())

' Convert local time
Dim localTime As New DateTimeOffset(timeComponent, _
                                    TimeZoneInfo.Local.GetUtcOffset(timeComponent))
returnedDate = ConvertFromDateTimeOffset(localTime)
Console.WriteLine("{0} converted to {1} {2}", _
                  localTime, _
                  returnedDate, _
                  returnedDate.Kind.ToString())

' Convert Central Standard Time
Dim cstTime As New DateTimeOffset(timeComponent, _
               TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time").GetUtcOffset(timeComponent))
returnedDate = ConvertFromDateTimeOffset(cstTime)
Console.WriteLine("{0} converted to {1} {2}", _
                  cstTime, _
                  returnedDate, _
                  returnedDate.Kind.ToString())
' The example displays the following output to the console:
'    6/19/2008 7:00:00 AM +00:00 converted to 6/19/2008 7:00:00 AM Utc
'    6/19/2008 7:00:00 AM -07:00 converted to 6/19/2008 7:00:00 AM Local
'    6/19/2008 7:00:00 AM -05:00 converted to 6/19/2008 7:00:00 AM Unspecified

注意

このコードは次の 2 つのことを前提としますが、アプリケーションおよびその日時値のソースによっては常に有効とは限りません。

  • オフセットが TimeSpan.Zero である日時値が UTC を表すことを前提とします。 実際には、UTC は特定のタイム ゾーンの時刻ではなく、世界のタイム ゾーンの時刻を標準化する際に基準となる時刻です。 タイム ゾーンのオフセットが Zero である場合もあります。

  • オフセットがローカル タイム ゾーンのオフセットと等しい日時が、ローカル タイム ゾーンを表すことを前提とします。 日時値は元のタイム ゾーンとの関連付けが解除されているので、これは該当しない場合があります。日時は、同じオフセットを持つ別のタイム ゾーンに由来する可能性があります。

関連項目