다음을 통해 공유


DateTimeOffset.ToOffset(TimeSpan) 메서드

정의

현재 DateTimeOffset 개체의 값을 오프셋 값으로 지정된 날짜와 시간으로 변환합니다.

public:
 DateTimeOffset ToOffset(TimeSpan offset);
public DateTimeOffset ToOffset (TimeSpan offset);
member this.ToOffset : TimeSpan -> DateTimeOffset
Public Function ToOffset (offset As TimeSpan) As DateTimeOffset

매개 변수

offset
TimeSpan

DateTimeOffset 값을 변환할 대상 오프셋입니다.

반환

원래 DateTimeOffset 개체와 같지만, 즉 해당 ToUniversalTime() 메서드가 동일한 시각을 반환하지만, Offset 속성이 offset으로 설정되어 있는 개체입니다.

예외

결과 DateTimeOffset 개체의 값은 DateTimeDateTimeOffset.MinValue보다 이전입니다.

또는

결과 DateTimeOffset 개체의 값은 DateTimeDateTimeOffset.MaxValue보다 낮습니다.

offset이 -14시간보다 작은 경우

또는

offset이 14시간보다 큰 경우

예제

다음 예제에서는 사용 하는 방법을 보여 줍니다.는 ToOffset 다른 오프셋을 사용 하 여 개체에 개체를 DateTimeOffset 변환 DateTimeOffset 하는 방법입니다.

using System;

public class DateTimeOffsetConversion
{
   private static DateTimeOffset sourceTime;

   public static void Main()
   {
      DateTimeOffset targetTime;
      sourceTime = new DateTimeOffset(2007, 9, 1, 9, 30, 0,
                                      new TimeSpan(-5, 0, 0));

      // Convert to same time (return sourceTime unchanged)
      targetTime = sourceTime.ToOffset(new TimeSpan(-5, 0, 0));
      ShowDateAndTimeInfo(targetTime);

      // Convert to UTC (0 offset)
      targetTime = sourceTime.ToOffset(TimeSpan.Zero);
      ShowDateAndTimeInfo(targetTime);

      // Convert to 8 hours behind UTC
      targetTime = sourceTime.ToOffset(new TimeSpan(-8, 0, 0));
      ShowDateAndTimeInfo(targetTime);

      // Convert to 3 hours ahead of UTC
      targetTime = sourceTime.ToOffset(new TimeSpan(3, 0, 0));
      ShowDateAndTimeInfo(targetTime);
   }

   private static void ShowDateAndTimeInfo(DateTimeOffset newTime)
   {
      Console.WriteLine("{0} converts to {1}", sourceTime, newTime);
      Console.WriteLine("{0} and {1} are equal: {2}",
                        sourceTime, newTime, sourceTime.Equals(newTime));
      Console.WriteLine("{0} and {1} are identical: {2}",
                        sourceTime, newTime,
                        sourceTime.EqualsExact(newTime));
      Console.WriteLine();
   }
}
//
// The example displays the following output:
//    9/1/2007 9:30:00 AM -05:00 converts to 9/1/2007 9:30:00 AM -05:00
//    9/1/2007 9:30:00 AM -05:00 and 9/1/2007 9:30:00 AM -05:00 are equal: True
//    9/1/2007 9:30:00 AM -05:00 and 9/1/2007 9:30:00 AM -05:00 are identical: True
//
//    9/1/2007 9:30:00 AM -05:00 converts to 9/1/2007 2:30:00 PM +00:00
//    9/1/2007 9:30:00 AM -05:00 and 9/1/2007 2:30:00 PM +00:00 are equal: True
//    9/1/2007 9:30:00 AM -05:00 and 9/1/2007 2:30:00 PM +00:00 are identical: False
//
//    9/1/2007 9:30:00 AM -05:00 converts to 9/1/2007 6:30:00 AM -08:00
//    9/1/2007 9:30:00 AM -05:00 and 9/1/2007 6:30:00 AM -08:00 are equal: True
//    9/1/2007 9:30:00 AM -05:00 and 9/1/2007 6:30:00 AM -08:00 are identical: False
//
//    9/1/2007 9:30:00 AM -05:00 converts to 9/1/2007 5:30:00 PM +03:00
//    9/1/2007 9:30:00 AM -05:00 and 9/1/2007 5:30:00 PM +03:00 are equal: True
//    9/1/2007 9:30:00 AM -05:00 and 9/1/2007 5:30:00 PM +03:00 are identical: False
open System

let sourceTime = DateTimeOffset(2007, 9, 1, 9, 30, 0, TimeSpan(-5, 0, 0))

let showDateAndTimeInfo newTime =
    printfn $"{sourceTime} converts to {newTime}"
    printfn $"{sourceTime} and {newTime} are equal: {sourceTime.Equals newTime}"
    printfn $"{sourceTime} and {newTime} are identical: {sourceTime.EqualsExact newTime}\n"

[<EntryPoint>]
let main _ =
    // Convert to same time (return sourceTime unchanged)
    let targetTime = sourceTime.ToOffset(TimeSpan(-5, 0, 0))
    showDateAndTimeInfo targetTime

    // Convert to UTC (0 offset)
    let targetTime = sourceTime.ToOffset TimeSpan.Zero
    showDateAndTimeInfo targetTime

    // Convert to 8 hours behind UTC
    let targetTime = sourceTime.ToOffset(TimeSpan(-8, 0, 0))
    showDateAndTimeInfo targetTime

    // Convert to 3 hours ahead of UTC
    let targetTime = sourceTime.ToOffset(TimeSpan(3, 0, 0))
    showDateAndTimeInfo targetTime

    0

// The example displays the following output:
//    9/1/2007 9:30:00 AM -05:00 converts to 9/1/2007 9:30:00 AM -05:00
//    9/1/2007 9:30:00 AM -05:00 and 9/1/2007 9:30:00 AM -05:00 are equal: True
//    9/1/2007 9:30:00 AM -05:00 and 9/1/2007 9:30:00 AM -05:00 are identical: True
//
//    9/1/2007 9:30:00 AM -05:00 converts to 9/1/2007 2:30:00 PM +00:00
//    9/1/2007 9:30:00 AM -05:00 and 9/1/2007 2:30:00 PM +00:00 are equal: True
//    9/1/2007 9:30:00 AM -05:00 and 9/1/2007 2:30:00 PM +00:00 are identical: False
//
//    9/1/2007 9:30:00 AM -05:00 converts to 9/1/2007 6:30:00 AM -08:00
//    9/1/2007 9:30:00 AM -05:00 and 9/1/2007 6:30:00 AM -08:00 are equal: True
//    9/1/2007 9:30:00 AM -05:00 and 9/1/2007 6:30:00 AM -08:00 are identical: False
//
//    9/1/2007 9:30:00 AM -05:00 converts to 9/1/2007 5:30:00 PM +03:00
//    9/1/2007 9:30:00 AM -05:00 and 9/1/2007 5:30:00 PM +03:00 are equal: True
//    9/1/2007 9:30:00 AM -05:00 and 9/1/2007 5:30:00 PM +03:00 are identical: False
Module DateTimeOffsetConversion
   Private sourceTime As New DateTimeOffset(#9/1/2007 9:30AM#, _
                                            New TimeSpan(-5, 0, 0))
   
   Public Sub Main()
      Dim targetTime As DateTimeOffset
      
      ' Convert to same time (return sourceTime unchanged)
      targetTime = sourceTime.ToOffset(New TimeSpan(-5, 0, 0))
      ShowDateAndTimeInfo(targetTime)
      
      ' Convert to UTC (0 offset)
      targetTime = sourceTime.ToOffset(TimeSpan.Zero)
      ShowDateAndTimeInfo(targetTime)
      
      ' Convert to 8 hours behind UTC
      targetTime = sourceTime.ToOffset(New TimeSpan(-8, 0, 0))
      ShowDateAndTimeInfo(targetTime)
      
      ' Convert to 3 hours ahead of UTC
      targetTime = sourceTime.ToOffset(New TimeSpan(3, 0, 0))
      ShowDateAndTimeInfo(targetTime)
   End Sub
   
   Private Sub ShowDateAndTimeInfo(newTime As DateTimeOffset)
      Console.WriteLine("{0} converts to {1}", sourceTime, newTime)
      Console.WriteLine("{0} and {1} are equal: {2}", _
                        sourceTime, newTime, sourceTime.Equals(newTime))
      Console.WriteLine("{0} and {1} are identical: {2}", _
                        sourceTime, newTime, _
                        sourceTime.EqualsExact(newTime)) 
      Console.WriteLine()
   End Sub
End Module
'
' The example displays the following output:
'    9/1/2007 9:30:00 AM -05:00 converts to 9/1/2007 9:30:00 AM -05:00
'    9/1/2007 9:30:00 AM -05:00 and 9/1/2007 9:30:00 AM -05:00 are equal: True
'    9/1/2007 9:30:00 AM -05:00 and 9/1/2007 9:30:00 AM -05:00 are identical: True
'    
'    9/1/2007 9:30:00 AM -05:00 converts to 9/1/2007 2:30:00 PM +00:00
'    9/1/2007 9:30:00 AM -05:00 and 9/1/2007 2:30:00 PM +00:00 are equal: True
'    9/1/2007 9:30:00 AM -05:00 and 9/1/2007 2:30:00 PM +00:00 are identical: False
'    
'    9/1/2007 9:30:00 AM -05:00 converts to 9/1/2007 6:30:00 AM -08:00
'    9/1/2007 9:30:00 AM -05:00 and 9/1/2007 6:30:00 AM -08:00 are equal: True
'    9/1/2007 9:30:00 AM -05:00 and 9/1/2007 6:30:00 AM -08:00 are identical: False
'    
'    9/1/2007 9:30:00 AM -05:00 converts to 9/1/2007 5:30:00 PM +03:00
'    9/1/2007 9:30:00 AM -05:00 and 9/1/2007 5:30:00 PM +03:00 are equal: True
'    9/1/2007 9:30:00 AM -05:00 and 9/1/2007 5:30:00 PM +03:00 are identical: False

설명

메서드는 ToOffset 메서드를 호출하는 대신 사용할 수 있습니다 TimeZoneInfo.ConvertTime(DateTimeOffset, TimeZoneInfo) . UTC(협정 세계시)의 표준 시간대 오프셋이 알려진 경우 한 표준 시간대에서 다른 표준 시간대로 간단한 변환을 수행하는 데 유용할 수 있습니다. 그러나 원래 DateTimeOffset 개체나 메서드 호출에서 반환된 새 DateTimeOffset 개체가 특정 표준 시간대와 명확하게 관련되지 않으므로 메서드는 변환에 표준 시간대 조정 규칙을 적용하지 않습니다.

적용 대상