asp.net.ph

DateTime.ToUniversalTime Method

System Namespace   DateTime Structure


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

[ VB ]
<Serializable>
Public Function ToUniversalTime ( ) As DateTime

[ C# ]
[ Serializable ]
public DateTime ToUniversalTime ( );

[ C++ ]
[ Serializable ]
public: DateTime ToUniversalTime ( );

[ JScript ]
public Serializable
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

The below code snippet demonstrates using the ToUniversalTime.

[ VB ]
System.Response.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.Response.WriteLine ( "Invalid format." )
End Try

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

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

System.Response.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.Response.WriteLine ( "Invalid format." )
End Try

localDateTime = univDateTime.ToLocalTime ( )

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

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

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

System.DateTime univDateTime = localDateTime.ToUniversalTime ( );

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

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

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

localDateTime = univDateTime.ToLocalTime ( );

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

DateTime Members   ToLocalTime Skip Navigation Links




Home
Suggested Reading


Previous page Back to top Next page

© 2000-2010 Rey Nuñez All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note

You can help support asp.net.ph