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 값을 현지 시간으로 여러 번 변환하는 방법을 보여 줍니다. 마지막 세 번은 모두 모호합니다. 이 속성은 모두 태평양 표준 시간대의 단일 날짜 및 시간에 매핑됩니다.

 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 반환 DateTime 된 개체DateTimeKind.LocalKind 속성 값을 설정하여 속성과 다릅니다.

적용 대상

추가 정보