DateTime.ToUniversalTime Method
Converts the current local time to coordinated universal time (UTC).
[Visual Basic] Public Function ToUniversalTime() As DateTime [C#] public DateTime ToUniversalTime(); [C++] public: DateTime ToUniversalTime(); [JScript] public function ToUniversalTime() : DateTime;
Return Value
The UTC DateTime equivalent to the current local time.
-or-
MaxValue if the current local time is too large to be represented as a DateTime.
-or-
MinValue if the current local time is too small to be represented as a DateTime.
Remarks
The UTC time is equal to the local time minus the UTC offset. For more information about the UTC offset, see TimeZone.GetUtcOffset.
This method assumes that the current DateTime holds the local time value, and not a UTC time. Therefore, each time it is run, the current method performs the necessary modifications on the DateTime to derive the UTC time, whether the current DateTime holds the local time or not.
This method always uses the local time zone when making calculations.
Example
[Visual Basic, C#, C++] The following sample demonstrates ToUniversalTime.
[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
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 | ToLocalTime