DateTime.ToLocalTime Method
Assembly: mscorlib (in mscorlib.dll)
public DateTime ToLocalTime ()
public function ToLocalTime () : DateTime
Return Value
A DateTime 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.The local time is equal to the UTC time plus the UTC offset. For more information about the UTC offset, see TimeZone.GetUtcOffset. The conversion also takes into account the daylight saving time rule that applies to the time represented by the current DateTime object.
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 | This instance of DateTime is converted to local time. |
| Local | No conversion is performed. |
| This instance of DateTime is assumed to be a UTC time, and the conversion is performed as if Kind were Utc. |
The value returned by the conversion is a DateTime whose Kind property always returns Local. Consequently, a valid result is returned even if ToLocalTime is applied repeatedly to the same DateTime.
This method always uses the local time zone when making calculations.
The following code example demonstrates the ToLocalTime method.
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 );
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 exp) {
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 exp) {
System.Console.WriteLine("Invalid format.");
return;
}
localDateTime = univDateTime.ToLocalTime();
System.Console.WriteLine("{0} universal time is {1} local time.",
univDateTime, localDateTime);
The following code example uses the SpecifyKind method to demonstrate how the Kind property influences the ToLocalTime and ToUniversalTime conversion methods.
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.