Export (0) Print
Expand All

DateTime.ToLocalTime Method

Converts the current coordinated universal time (UTC) to local time.

[Visual Basic]
Public Function ToLocalTime() As DateTime
[C#]
public DateTime ToLocalTime();
[C++]
public: DateTime ToLocalTime();
[JScript]
public function ToLocalTime() : DateTime;

Return Value

The DateTime equivalent to the current UTC time, adjusted to the local time zone and daylight saving time.

-or-

MaxValue if the current UTC time is too large to be represented as a DateTime.

-or-

MinValue if the current UTC time is too small to be represented as a DateTime.

Remarks

This method assumes that the current DateTime holds the UTC time value, and not a local time. Therefore, each time it is run, the current method performs the necessary modifications on the DateTime to derive the local time, whether the current DateTime holds the UTC time or not.

This method always uses the local time zone when making calculations.

Example

[Visual Basic, C#, C++] The following sample demonstrates ToLocalTime.

[Visual Basic] 
System.Console.WriteLine("Enter a date and time.")
Dim strDateTime As String
strDateTime = System.Console.ReadLine()

Dim localDateTime As System.DateTime
Try
   localDateTime = System.DateTime.Parse(strDateTime)
Catch exp As System.FormatException
   System.Console.WriteLine("Invalid format.")
End Try

Dim univDateTime As System.DateTime
univDateTime = localDateTime.ToUniversalTime()

System.Console.WriteLine("{0} local time is {1} universal time.", _
                           localDateTime, _
                           univDateTime)

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

Try
   univDateTime = System.DateTime.Parse(strDateTime)
Catch exp As System.FormatException
   System.Console.WriteLine("Invalid format.")
End Try

localDateTime = univDateTime.ToLocalTime()

System.Console.WriteLine("{0} universal time is {1} local time.", _
                           univDateTime, _
                           localDateTime)

[C#] 
System.Console.WriteLine("Enter a date and time.");
string strDateTime = System.Console.ReadLine();

System.DateTime localDateTime;
try {
    localDateTime = System.DateTime.Parse(strDateTime);
}
catch (System.FormatException) {
    System.Console.WriteLine("Invalid format.");
    return;
}

System.DateTime univDateTime = localDateTime.ToUniversalTime();

System.Console.WriteLine("{0} local time is {1} universal time.",
                         localDateTime,
                         univDateTime); 

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

try {
    univDateTime = System.DateTime.Parse(strDateTime);
}
catch (System.FormatException) {
    System.Console.WriteLine("Invalid format.");
    return;
}

localDateTime = univDateTime.ToLocalTime();

System.Console.WriteLine("{0} universal time is {1} local time.",
                         univDateTime,
                         localDateTime); 

[C++] 
System::Console::WriteLine(S"Enter a date and time.");
String* strDateTime = System::Console::ReadLine();

System::DateTime localDateTime;
try {
   localDateTime = System::DateTime::Parse(strDateTime);
} catch (System::FormatException*) {
   System::Console::WriteLine(S"Invalid format.");
   return;
}

System::DateTime univDateTime = localDateTime.ToUniversalTime();

System::Console::WriteLine(S"{0} local time is {1} universal time.", __box(localDateTime), __box(univDateTime));

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

try {
   univDateTime = System::DateTime::Parse(strDateTime);
} catch (System::FormatException*) {
   System::Console::WriteLine(S"Invalid format.");
   return;
}

localDateTime = univDateTime.ToLocalTime();

System::Console::WriteLine(S"{0} universal time is {1} local time.", __box(univDateTime), __box(localDateTime));

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

DateTime Structure | DateTime Members | System Namespace | TimeZone | GetUtcOffset | TimeZone.GetDaylightChanges | ToUniversalTime

Show:
© 2014 Microsoft