DateTime.ToLocalTime 메서드

정의

현재 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

반환

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 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 속성이 ToLocalTime 및 변환 메서드에 영향을 주는 방법을 보여 줍니다 ToUniversalTime .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 오프셋에 대 한 자세한 내용은을 참조 하십시오 TimeZoneInfo.GetUtcOffset .For more information about the UTC offset, see TimeZoneInfo.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 XP 시스템에서 메서드는 ToLocalTime UTC에서 현지 시간으로 변환할 때 현재 조정 규칙만 인식 합니다.On Windows 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.

종류Kind 결과Results
Utc 이 인스턴스 DateTime 는 현지 시간으로 변환 됩니다.This instance of DateTime is converted to local time.
Local 변환이 수행 되지 않습니다.No conversion is performed.
Unspecified 이 인스턴스 DateTime 는 UTC 시간으로 간주 되며, 변환은가 인 것 처럼 수행 됩니다 Kind Utc .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.

예를 들어 표준 시간에서 일광 절약 시간으로의 전환은 2010 년 3 월 14 일 오전 2:00, 오전 시, 오전 시, 오전 3:00 시, 오전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.

[! datetime.tolocaltime# 1] [! code-vbdatetime.tolocaltime # 1][!code-csharpSystem.DateTime.ToLocalTime#1] [!code-vbSystem.DateTime.ToLocalTime#1]

적용 대상

추가 정보