DateTimeOffset.LocalDateTime プロパティ

定義

現在の DateTime オブジェクトの現地日時を表す DateTimeOffset 値を取得します。

public:
 property DateTime LocalDateTime { DateTime get(); };
public DateTime LocalDateTime { get; }
member this.LocalDateTime : DateTime
Public ReadOnly Property LocalDateTime As DateTime

プロパティ値

DateTime

現在の DateTimeOffset オブジェクトの現地日時。

次の例は、米国太平洋標準時ゾーンでの値の DateTimeOffset ローカル時刻への変換をいくつか示しています。 最後の 3 回はすべてあいまいであることに注意してください。このプロパティは、それらすべてを太平洋標準タイム ゾーンの 1 つの日付と時刻にマップします。

 DateTimeOffset dto;

 // Current time
 dto = DateTimeOffset.Now;
 Console.WriteLine(dto.LocalDateTime);
 // UTC time
 dto = DateTimeOffset.UtcNow;
 Console.WriteLine(dto.LocalDateTime);

// Transition to DST in local time zone occurs on 3/11/2007 at 2:00 AM
 dto = new DateTimeOffset(2007, 3, 11, 3, 30, 0, new TimeSpan(-7, 0, 0));
 Console.WriteLine(dto.LocalDateTime);
 dto = new DateTimeOffset(2007, 3, 11, 2, 30, 0, new TimeSpan(-7, 0, 0));
 Console.WriteLine(dto.LocalDateTime);
 // Invalid time in local time zone
 dto = new DateTimeOffset(2007, 3, 11, 2, 30, 0, new TimeSpan(-8, 0, 0));
 Console.WriteLine(TimeZoneInfo.Local.IsInvalidTime(dto.DateTime));
 Console.WriteLine(dto.LocalDateTime);

 // Transition from DST in local time zone occurs on 11/4/07 at 2:00 AM
 // This is an ambiguous time
 dto = new DateTimeOffset(2007, 11, 4, 1, 30, 0, new TimeSpan(-7, 0, 0));
 Console.WriteLine(TimeZoneInfo.Local.IsAmbiguousTime(dto));
 Console.WriteLine(dto.LocalDateTime);
 dto = new DateTimeOffset(2007, 11, 4, 2, 30, 0, new TimeSpan(-7, 0, 0));
 Console.WriteLine(TimeZoneInfo.Local.IsAmbiguousTime(dto));
 Console.WriteLine(dto.LocalDateTime);
 // This is also an ambiguous time
 dto = new DateTimeOffset(2007, 11, 4, 1, 30, 0, new TimeSpan(-8, 0, 0));
 Console.WriteLine(TimeZoneInfo.Local.IsAmbiguousTime(dto));
 Console.WriteLine(dto.LocalDateTime);
 // If run on 3/8/2007 at 4:56 PM, the code produces the following
 // output:
 //    3/8/2007 4:56:03 PM
 //    3/8/2007 4:56:03 PM
 //    3/11/2007 3:30:00 AM
 //    3/11/2007 1:30:00 AM
 //    True
 //    3/11/2007 3:30:00 AM
 //    True
 //    11/4/2007 1:30:00 AM
 //    11/4/2007 1:30:00 AM
 //    True
 //    11/4/2007 1:30:00 AM
// Current time
let dto = DateTimeOffset.Now
printfn $"{dto.LocalDateTime}"
// UTC time
let dto = DateTimeOffset.UtcNow
printfn $"{dto.LocalDateTime}"

// Transition to DST in local time zone occurs on 3/11/2007 at 2:00 AM
let dto = DateTimeOffset(2007, 3, 11, 3, 30, 0, TimeSpan(-7, 0, 0))
printfn $"{dto.LocalDateTime}"
let dto = DateTimeOffset(2007, 3, 11, 2, 30, 0, TimeSpan(-7, 0, 0))
printfn $"{dto.LocalDateTime}"
// Invalid time in local time zone
let dto = DateTimeOffset(2007, 3, 11, 2, 30, 0, TimeSpan(-8, 0, 0))
printfn $"{TimeZoneInfo.Local.IsInvalidTime dto.DateTime}"
printfn $"{dto.LocalDateTime}"

// Transition from DST in local time zone occurs on 11/4/07 at 2:00 AM
// This is an ambiguous time
let dto = DateTimeOffset(2007, 11, 4, 1, 30, 0, TimeSpan(-7, 0, 0))
printfn $"{TimeZoneInfo.Local.IsAmbiguousTime dto}"
printfn $"{dto.LocalDateTime}"
let dto = DateTimeOffset(2007, 11, 4, 2, 30, 0, TimeSpan(-7, 0, 0))
printfn $"{TimeZoneInfo.Local.IsAmbiguousTime dto}"
printfn $"{dto.LocalDateTime}"
// This is also an ambiguous time
let dto = DateTimeOffset(2007, 11, 4, 1, 30, 0, TimeSpan(-8, 0, 0))
printfn $"{TimeZoneInfo.Local.IsAmbiguousTime dto}"
printfn $"{dto.LocalDateTime}"

// If run on 3/8/2007 at 4:56 PM, the code produces the following
// output:
//    3/8/2007 4:56:03 PM
//    3/8/2007 4:56:03 PM
//    3/11/2007 3:30:00 AM
//    3/11/2007 1:30:00 AM
//    True
//    3/11/2007 3:30:00 AM
//    True
//    11/4/2007 1:30:00 AM
//    11/4/2007 1:30:00 AM
//    True
//    11/4/2007 1:30:00 AM
 Dim dto As DateTimeOffset

 ' Current time
 dto = DateTimeOffset.Now
 Console.WriteLine(dto.LocalDateTime)
 ' UTC time
 dto = DateTimeOffset.UtcNow
 Console.WriteLine(dto.LocalDateTime)

' Transition to DST in local time zone occurs on 3/11/2007 at 2:00 AM
 dto = New DateTimeOffset(#03/11/2007 3:30AM#, New Timespan(-7, 0, 0))      
 Console.WriteLine(dto.LocalDateTime)
 dto = New DateTimeOffset(#03/11/2007 2:30AM#, New Timespan(-7, 0, 0))
 Console.WriteLine(dto.LocalDateTime)
 ' Invalid time in local time zone
 dto = New DateTimeOffset(#03/11/2007 2:30AM#, New Timespan(-8, 0, 0))
 Console.WriteLine(TimeZoneInfo.Local.IsInvalidTime(dto.DateTime))
 Console.WriteLine(dto.LocalDateTime)

 ' Transition from DST in local time zone occurs on 11/4/07 at 2:00 AM
 ' This is an ambiguous time
 dto = New DateTimeOffset(#11/4/2007 1:30AM#, New TimeSpan(-7, 0, 0))
 Console.WriteLine(TimeZoneInfo.Local.IsAmbiguousTime(dto))
 Console.WriteLine(dto.LocalDateTime)
 dto = New DateTimeOffset(#11/4/2007 2:30AM#, New TimeSpan(-7, 0, 0))           
 Console.WriteLine(TimeZoneInfo.Local.IsAmbiguousTime(dto))
 Console.WriteLine(dto.LocalDateTime)
 ' This is also an ambiguous time
 dto = New DateTimeOffset(#11/4/2007 1:30AM#, New TimeSpan(-8, 0, 0))           
 Console.WriteLine(TimeZoneInfo.Local.IsAmbiguousTime(dto))
 Console.WriteLine(dto.LocalDateTime)
 ' If run on 3/8/2007 at 4:56 PM, the code produces the following
 ' output:
 '    3/8/2007 4:56:03 PM
 '    3/8/2007 4:56:03 PM
 '    3/11/2007 3:30:00 AM
 '    3/11/2007 1:30:00 AM
 '    True
 '    3/11/2007 3:30:00 AM
 '    True
 '    11/4/2007 1:30:00 AM
 '    11/4/2007 1:30:00 AM
 '    True
 '    11/4/2007 1:30:00 AM

注釈

必要に応じて、プロパティは LocalDateTime 現在 DateTimeOffset のオブジェクトの日付と時刻をローカル システムの日付と時刻に変換します。 変換は 2 段階の操作です。

  1. このプロパティは、現在 DateTimeOffset のオブジェクトの時刻を協定世界時 (UTC) に変換します。

  2. その後、このプロパティは UTC をローカル時刻に変換します。

無効な時刻はなく、あいまいな時刻はローカル ゾーンの標準時刻にマップされます。 (ただし、変換によって異常が発生する可能性があります。ローカル コンピューターから派生した値にあいまいな日時が反映されている場合 DateTimeOffset は、その値を UTC に変換し、元の時刻とは異なるローカル時刻に戻すことができます)。 このプロパティは、この変換を実行するときに、ローカル タイム ゾーンのすべての調整規則を適用します。

このプロパティは、変換に役立つDateTimeOffsetオブジェクトの日付と時刻の両方のDateTimeOffsetコンポーネントをDateTime返します。 このプロパティは、必要な時間変換を実行するだけでなく、返されるDateTimeオブジェクトDateTimeKind.LocalDateTimeプロパティのKind値を .

適用対象

こちらもご覧ください