DateTime.ToLocalTime DateTime.ToLocalTime DateTime.ToLocalTime DateTime.ToLocalTime Method

정의

현재 DateTime 개체의 값을 현지 시간으로 변환합니다.Converts the value of the current DateTime object to local time.

public:
 DateTime ToLocalTime();
public DateTime ToLocalTime ();
member this.ToLocalTime : unit -> DateTime
Public Function ToLocalTime () As DateTime

반환

Kind 속성이 Local이고, 현재 DateTime 개체의 값에 해당하는 현지 시간이 값인 개체입니다. 변환된 값이 너무 커서 MaxValue 개체로 표현할 수 없는 경우 DateTime이고, 변환된 값이 너무 작아서 MinValue 개체로 표현할 수 없는 경우 DateTime입니다.An object whose Kind property is Local, and whose value is the local time equivalent to the value of the current DateTime object, or MaxValue if the converted value is too large to be represented by a DateTime object, or MinValue if the converted value is too small to be represented as a DateTime object.

예제

다음 예제는 ToLocalTime 메서드.The following example demonstrates the ToLocalTime method. Note 정확한 출력 현재 문화권 및 실행 되는 시스템의 현지 표준 시간대에 따라 달라 집니다.Note that the exact output depends on the current culture and the local time zone of the system on which it is run.

using namespace System;

void main()
{
   Console::WriteLine("Enter a date and time.");
   String^ strDateTime = Console::ReadLine();

   DateTime localDateTime, univDateTime;
   try
   {
      localDateTime = DateTime::Parse(strDateTime);
      univDateTime = localDateTime.ToUniversalTime();
    
      Console::WriteLine("{0} local time is {1} universal time.",
                         localDateTime, univDateTime );
   }
   catch (FormatException^) 
   {
      Console::WriteLine("Invalid format.");
      return;
   }

   Console::WriteLine("Enter a date and time in universal time.");
   strDateTime = Console::ReadLine();

   try
   {
      univDateTime = DateTime::Parse(strDateTime);
      localDateTime = univDateTime.ToLocalTime();
  
      Console::WriteLine("{0} universal time is {1} local time.",
                         univDateTime, localDateTime );
   }
   catch (FormatException^) 
   {
      Console::WriteLine("Invalid format.");
      return;
   }
}
// The example displays output like the following when run on a 
// computer whose culture is en-US in the Pacific Standard Time zone:
//     Enter a date and time.
//     12/10/2015 6:18 AM
//     12/10/2015 6:18:00 AM local time is 12/10/2015 2:18:00 PM universal time.
//     Enter a date and time in universal time.
//     12/20/2015 6:42:00
//     12/20/2015 6:42:00 AM universal time is 12/19/2015 10:42:00 PM local time.
using System;

class Example
{
    static void Main()
    {
        DateTime localDateTime, univDateTime;
        
        Console.WriteLine("Enter a date and time.");
        string strDateTime = Console.ReadLine();

        try {
            localDateTime = DateTime.Parse(strDateTime);
            univDateTime = localDateTime.ToUniversalTime();
    
            Console.WriteLine("{0} local time is {1} universal time.",
                                localDateTime,
                                    univDateTime); 
        }
        catch (FormatException) {
            Console.WriteLine("Invalid format.");
            return;
        }

        Console.WriteLine("Enter a date and time in universal time.");
        strDateTime = Console.ReadLine();

        try {
            univDateTime = DateTime.Parse(strDateTime);
            localDateTime = univDateTime.ToLocalTime();
    
            Console.WriteLine("{0} universal time is {1} local time.",
                                     univDateTime,
                                     localDateTime); 
        }
        catch (FormatException) {
            Console.WriteLine("Invalid format.");
            return;
        }

    }
}
// The example displays output like the following when run on a 
// computer whose culture is en-US in the Pacific Standard Time zone:
//     Enter a date and time.
//     12/10/2015 6:18 AM
//     12/10/2015 6:18:00 AM local time is 12/10/2015 2:18:00 PM universal time.
//     Enter a date and time in universal time.
//     12/20/2015 6:42:00
//     12/20/2015 6:42:00 AM universal time is 12/19/2015 10:42:00 PM local time.
Module Example
    Sub Main()
      Dim localDateTime, univDateTime As DateTime
      
      Console.WriteLine("Enter a date and time.")
      Dim strDateTime As String = Console.ReadLine()
      Try
         localDateTime = DateTime.Parse(strDateTime)
         univDateTime = localDateTime.ToUniversalTime()
         Console.WriteLine("{0} local time is {1} universal time.", 
                           localDateTime, univDateTime)
      Catch exp As FormatException
         Console.WriteLine("Invalid format.")
      End Try

      Console.WriteLine("Enter a date and time in universal time.")
      strDateTime = Console.ReadLine()
      Try
         univDateTime = DateTime.Parse(strDateTime)
         localDateTime = univDateTime.ToLocalTime()
  
         Console.WriteLine("{0} universal time is {1} local time.", _
                           univDateTime, localDateTime)
      Catch exp As FormatException
         Console.WriteLine("Invalid format.")
      End Try
    End Sub
End Module
' The example displays output like the following when run on a 
' computer whose culture is en-US in the Pacific Standard Time zone:
'     Enter a date and time.
'     12/10/2015 6:18 AM
'     12/10/2015 6:18:00 AM local time is 12/10/2015 2:18:00 PM universal time.
'     Enter a date and time in universal time.
'     12/20/2015 6:42:00
'     12/20/2015 6:42:00 AM universal time is 12/19/2015 10:42:00 PM local time.

다음 예제에서는 합니다 SpecifyKind 메서드를 보여 줍니다 하는 방법을 Kind 속성이 영향을 줍니다 합니다 ToLocalTimeToUniversalTime 변환 메서드.The following example uses the SpecifyKind method to demonstrate how the Kind property influences the ToLocalTime and ToUniversalTime conversion methods.

// This code example demonstrates the DateTime Kind, Now, and
// UtcNow properties, and the SpecifyKind(), ToLocalTime(), 
// and ToUniversalTime() methods.

using System;

class Sample 
{
    public static void Main() 
    {
// Get the date and time for the current moment, adjusted 
// to the local time zone.

    DateTime saveNow = DateTime.Now;

// Get the date and time for the current moment expressed 
// as coordinated universal time (UTC).

    DateTime saveUtcNow = DateTime.UtcNow;
    DateTime myDt;

// Display the value and Kind property of the current moment 
// expressed as UTC and local time.

    DisplayNow("UtcNow: ..........", saveUtcNow);
    DisplayNow("Now: .............", saveNow);
    Console.WriteLine();

// Change the Kind property of the current moment to 
// DateTimeKind.Utc and display the result.

    myDt = DateTime.SpecifyKind(saveNow, DateTimeKind.Utc);
    Display("Utc: .............", myDt);

// Change the Kind property of the current moment to 
// DateTimeKind.Local and display the result.

    myDt = DateTime.SpecifyKind(saveNow, DateTimeKind.Local);
    Display("Local: ...........", myDt);

// Change the Kind property of the current moment to 
// DateTimeKind.Unspecified and display the result.

    myDt = DateTime.SpecifyKind(saveNow, DateTimeKind.Unspecified);
    Display("Unspecified: .....", myDt);
    }

// Display the value and Kind property of a DateTime structure, the 
// DateTime structure converted to local time, and the DateTime 
// structure converted to universal time. 

    public static string datePatt = @"M/d/yyyy hh:mm:ss tt";
    public static void Display(string title, DateTime inputDt)
    {
    DateTime dispDt = inputDt;
    string dtString;

// Display the original DateTime.

    dtString = dispDt.ToString(datePatt);
    Console.WriteLine("{0} {1}, Kind = {2}", 
                      title, dtString, dispDt.Kind);

// Convert inputDt to local time and display the result. 
// If inputDt.Kind is DateTimeKind.Utc, the conversion is performed.
// If inputDt.Kind is DateTimeKind.Local, the conversion is not performed.
// If inputDt.Kind is DateTimeKind.Unspecified, the conversion is 
// performed as if inputDt was universal time.

    dispDt = inputDt.ToLocalTime();
    dtString = dispDt.ToString(datePatt);
    Console.WriteLine("  ToLocalTime:     {0}, Kind = {1}", 
                      dtString, dispDt.Kind);

// Convert inputDt to universal time and display the result. 
// If inputDt.Kind is DateTimeKind.Utc, the conversion is not performed.
// If inputDt.Kind is DateTimeKind.Local, the conversion is performed.
// If inputDt.Kind is DateTimeKind.Unspecified, the conversion is 
// performed as if inputDt was local time.

    dispDt = inputDt.ToUniversalTime();
    dtString = dispDt.ToString(datePatt);
    Console.WriteLine("  ToUniversalTime: {0}, Kind = {1}", 
                      dtString, dispDt.Kind);
    Console.WriteLine();
    }

// Display the value and Kind property for DateTime.Now and DateTime.UtcNow.

    public static void DisplayNow(string title, DateTime inputDt)
    {
    string dtString = inputDt.ToString(datePatt);
    Console.WriteLine("{0} {1}, Kind = {2}", 
                      title, dtString, inputDt.Kind);
    }
}

/*
This code example produces the following results:

UtcNow: .......... 5/6/2005 09:34:42 PM, Kind = Utc
Now: ............. 5/6/2005 02:34:42 PM, Kind = Local

Utc: ............. 5/6/2005 02:34:42 PM, Kind = Utc
  ToLocalTime:     5/6/2005 07:34:42 AM, Kind = Local
  ToUniversalTime: 5/6/2005 02:34:42 PM, Kind = Utc

Local: ........... 5/6/2005 02:34:42 PM, Kind = Local
  ToLocalTime:     5/6/2005 02:34:42 PM, Kind = Local
  ToUniversalTime: 5/6/2005 09:34:42 PM, Kind = Utc

Unspecified: ..... 5/6/2005 02:34:42 PM, Kind = Unspecified
  ToLocalTime:     5/6/2005 07:34:42 AM, Kind = Local
  ToUniversalTime: 5/6/2005 09:34:42 PM, Kind = Utc

*/

' This code example demonstrates the DateTime Kind, Now, and
' UtcNow properties, and the SpecifyKind(), ToLocalTime(), 
' and ToUniversalTime() methods.
Class Sample
    Public Shared Sub Main() 
        ' Get the date and time for the current moment, adjusted 
        ' to the local time zone.
        Dim saveNow As DateTime = DateTime.Now
        
        ' Get the date and time for the current moment expressed 
        ' as coordinated universal time (UTC).
        Dim saveUtcNow As DateTime = DateTime.UtcNow
        Dim myDt As DateTime
        
        ' Display the value and Kind property of the current moment 
        ' expressed as UTC and local time.
        DisplayNow("UtcNow: ..........", saveUtcNow)
        DisplayNow("Now: .............", saveNow)
        Console.WriteLine()
        
        ' Change the Kind property of the current moment to 
        ' DateTimeKind.Utc and display the result.
        myDt = DateTime.SpecifyKind(saveNow, DateTimeKind.Utc)
        Display("Utc: .............", myDt)
        
        ' Change the Kind property of the current moment to 
        ' DateTimeKind.Local and display the result.
        myDt = DateTime.SpecifyKind(saveNow, DateTimeKind.Local)
        Display("Local: ...........", myDt)
        
        ' Change the Kind property of the current moment to 
        ' DateTimeKind.Unspecified and display the result.
        myDt = DateTime.SpecifyKind(saveNow, DateTimeKind.Unspecified)
        Display("Unspecified: .....", myDt)
    End Sub
    
    ' Display the value and Kind property of a DateTime structure, the 
    ' DateTime structure converted to local time, and the DateTime 
    ' structure converted to universal time. 

    Public Shared datePatt As String = "M/d/yyyy hh:mm:ss tt"
    
    Public Shared Sub Display(ByVal title As String, ByVal inputDt As DateTime) 
        Dim dispDt As DateTime = inputDt
        Dim dtString As String
        
        ' Display the original DateTime.
        dtString = dispDt.ToString(datePatt)
        Console.WriteLine("{0} {1}, Kind = {2}", title, dtString, dispDt.Kind)
        
        ' Convert inputDt to local time and display the result. 
        ' If inputDt.Kind is DateTimeKind.Utc, the conversion is performed.
        ' If inputDt.Kind is DateTimeKind.Local, the conversion is not performed.
        ' If inputDt.Kind is DateTimeKind.Unspecified, the conversion is 
        ' performed as if inputDt was universal time.
        dispDt = inputDt.ToLocalTime()
        dtString = dispDt.ToString(datePatt)
        Console.WriteLine("  ToLocalTime:     {0}, Kind = {1}", dtString, dispDt.Kind)
        
        ' Convert inputDt to universal time and display the result. 
        ' If inputDt.Kind is DateTimeKind.Utc, the conversion is not performed.
        ' If inputDt.Kind is DateTimeKind.Local, the conversion is performed.
        ' If inputDt.Kind is DateTimeKind.Unspecified, the conversion is 
        ' performed as if inputDt was local time.
        dispDt = inputDt.ToUniversalTime()
        dtString = dispDt.ToString(datePatt)
        Console.WriteLine("  ToUniversalTime: {0}, Kind = {1}", dtString, dispDt.Kind)
        Console.WriteLine()
    End Sub
    
    
    ' Display the value and Kind property for DateTime.Now and DateTime.UtcNow.

    Public Shared Sub DisplayNow(ByVal title As String, ByVal inputDt As DateTime) 
        Dim dtString As String = inputDt.ToString(datePatt)
        Console.WriteLine("{0} {1}, Kind = {2}", title, dtString, inputDt.Kind)
    End Sub
End Class

'
'This code example produces the following results:
'
'UtcNow: .......... 5/6/2005 09:34:42 PM, Kind = Utc
'Now: ............. 5/6/2005 02:34:42 PM, Kind = Local
'
'Utc: ............. 5/6/2005 02:34:42 PM, Kind = Utc
'  ToLocalTime:     5/6/2005 07:34:42 AM, Kind = Local
'  ToUniversalTime: 5/6/2005 02:34:42 PM, Kind = Utc
'
'Local: ........... 5/6/2005 02:34:42 PM, Kind = Local
'  ToLocalTime:     5/6/2005 02:34:42 PM, Kind = Local
'  ToUniversalTime: 5/6/2005 09:34:42 PM, Kind = Utc
'
'Unspecified: ..... 5/6/2005 02:34:42 PM, Kind = Unspecified
'  ToLocalTime:     5/6/2005 07:34:42 AM, Kind = Local
'  ToUniversalTime: 5/6/2005 09:34:42 PM, Kind = Utc
'

설명

현지 시간 utc (협정 세계시) 시간 및 UTC 오프셋 하는 것과 같습니다.The local time is equal to the Coordinated Universal Time (UTC) time plus the UTC offset. UTC 오프셋에 대 한 자세한 내용은 참조 하세요. TimeZone.GetUtcOffset합니다.For more information about the UTC offset, see TimeZone.GetUtcOffset. 변환도 고려 현재 표시 된 시간이 적용 되는 일광 절약 시간 규칙 DateTime 개체입니다.The conversion also takes into account the daylight saving time rule that applies to the time represented by the current DateTime object.

중요

Windows XPWindows XP 체제는 ToLocalTime 메서드 UTC에서 현지 시간으로 변환 하는 경우에 현재 조정 규칙을 인식 합니다.On Windows XPWindows XP systems, the ToLocalTime method recognizes only the current adjustment rule when converting from UTC to local time. 결과적으로, 현재 조정 규칙이 적용 되기 기간에 대 한 변환 UTC와 현지 시간 사이의 차이 정확 하 게 반영 되지 않을 수 있습니다.As a result, conversions for periods before the current adjustment rule came into effect may not accurately reflect the difference between UTC and local time.

.NET Framework 버전 2.0에서 반환한 값을 사용 하 여 시작 합니다 ToLocalTime 메서드에 의해 결정 됩니다 합니다 Kind 속성이 현재 DateTime 개체입니다.Starting with the .NET Framework version 2.0, the value returned by the ToLocalTime method is determined by the Kind property of the current DateTime object. 다음 표에서 가능한 결과 설명 합니다.The following table describes the possible results.

KindKind 결과Results
Utc 이 인스턴스의 DateTime 현지 시간으로 변환 됩니다.This instance of DateTime is converted to local time.
Local 변환이 수행 되지 않습니다.No conversion is performed.
Unspecified 이 인스턴스의 DateTime UTC 시간으로 간주 됩니다 및 변환이 수행 되며 처럼 KindUtc합니다.This instance of DateTime is assumed to be a UTC time, and the conversion is performed as if Kind were Utc.

참고

ToLocalTime 변환 메서드를 DateTime UTC에서 현지 시간 사이의 값입니다.The ToLocalTime method converts a DateTime value from UTC to local time. 지정 된 표준 시간대의 시간을 현지 시간으로 변환 하려면 사용 된 TimeZoneInfo.ConvertTime 메서드.To convert the time in any designated time zone to local time, use the TimeZoneInfo.ConvertTime method.

변환에 의해 반환 되는 값을 DateTime 해당 Kind 속성은 항상 반환 Local합니다.The value returned by the conversion is a DateTime whose Kind property always returns Local. 유효한 결과 반환 하는 결과적으로 경우에 ToLocalTime 동일 하 게 반복적으로 적용 됩니다 DateTime합니다.Consequently, a valid result is returned even if ToLocalTime is applied repeatedly to the same DateTime.

호출자 참고

사용할 수는 ToLocalTime() 로컬 날짜 및 시간 값으로 UTC로 변환 된 복원 방법 합니다 ToUniversalTime() 또는 FromFileTimeUtc(Int64) 메서드.You can use the ToLocalTime() method to restore a local date and time value that was converted to UTC by the ToUniversalTime() or FromFileTimeUtc(Int64) method. 그러나 원래 시간이 현지 표준 시간대에서 잘못 된 시간을 나타내는, 복원 된 값을 일치 하지 않습니다.However, if the original time represents an invalid time in the local time zone, it will not match the restored value. 경우는 ToLocalTime() 메서드 시간으로 변환 된 UTC에서 현지 표준 시간대, 현지 표준 시간대에서 한 유효 하도록 시간을 조정 합니다.When the ToLocalTime() method converts a time from UTC to the local time zone, it also adjusts the time so that is valid in the local time zone.

표준 시간에서 일광 절약 시간 전환 미국에서 발생 하는 예를 들어, 오전 2 시, 오전 3 시에 한 시간 전진할 때 2010 년 3 월 14 일 태평양 표준 시간대For example, the transition from standard time to daylight saving time occurs in the U.S. Pacific Time zone on March 14, 2010, at 2:00 A.M., when the time advances by one hour, to 3:00 A.M. 이 시간 간격은이 표준 시간대에 존재 하지 않는 시간 간격, 잘못 된 시간입니다.This hour interval is an invalid time, that is, a time interval that does not exist in this time zone. 다음 예제에서는 내에 포함 되는 시간이이 범위에서 UTC로 변환 하는 ToUniversalTime() 메서드에서 복원 되는 ToLocalTime() 메서드를 원래 값은 시간으로 조정 되는 유효한.The following example shows that when a time that falls within this range is converted to UTC by the ToUniversalTime() method and is then restored by the ToLocalTime() method, the original value is adjusted to become a valid time. 여부를 특정 날짜 및 시간 값에 적용 될 수 있습니다 수정 전달 하 여 확인할 수 있습니다는 IsInvalidTime(DateTime) 메서드를 예로 보여 줍니다.You can determine whether a particular date and time value may be subject to modification by passing it to the IsInvalidTime(DateTime) method, as the example illustrates.

[! 코드 csharpSystem.DateTime.ToLocalTime#1] [! 코드 vbSystem.DateTime.ToLocalTime#1][!code-csharpSystem.DateTime.ToLocalTime#1] [!code-vbSystem.DateTime.ToLocalTime#1]

적용 대상

추가 정보