asp.net.ph

DateTime.ToLocalTime Method

System Namespace   DateTime Structure


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

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

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

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

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

The below code snippet demonstrates using the ToLocalTime.

[ 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   TimeZone   GetUtcOffset   TimeZone.GetDaylightChanges   ToUniversalTime 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