GetUtcOffset Method
.NET Framework Class Library
TimeZone..::.GetUtcOffset Method

Returns the Coordinated Universal Time (UTC) offset for the specified local time.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
Visual Basic (Declaration)
Public MustOverride Function GetUtcOffset ( _
    time As DateTime _
) As TimeSpan
Visual Basic (Usage)
Dim instance As TimeZone
Dim time As DateTime
Dim returnValue As TimeSpan

returnValue = instance.GetUtcOffset(time)
C#
public abstract TimeSpan GetUtcOffset(
    DateTime time
)
Visual C++
public:
virtual TimeSpan GetUtcOffset(
    DateTime time
) abstract
JScript
public abstract function GetUtcOffset(
    time : DateTime
) : TimeSpan

Parameters

time
Type: System..::.DateTime
A date and time value.

Return Value

Type: System..::.TimeSpan
The Coordinated Universal Time (UTC) offset from Time.

This method returns the offset, or difference, between the time parameter and Coordinated Universal Time (UTC). That is:

time = UTC + offset

The method interprets the time zone of time based on its Kind property. If the value of the Kind property is DateTimeKind..::.Local or DateTimeKind..::.Unspecified, the method returns the offset of the local time zone. If the value of the Kind property is DateTimeKind..::.Utc, the method returns an offset equal to TimeSpan..::.Zero.

If the local time zone observes daylight saving time, GetUtcOffset applies the current adjustment rule to time when determining the offset of the local time zone. That is, the offset returned by GetUtcOffset reflects whether time falls in the time zone's standard time or its daylight saving time.

NoteNote:

   The GetUtcOffset method recognizes only the current daylight saving time adjustment rule for the local time zone. As a result, it is guaranteed to accurately return the UTC offset of a local time only during the period in which the latest adjustment rule is in effect. It may return inaccurate results if Time is a historic date and time value that was subject to a previous adjustment rule.

The GetUtcOffset method corresponds to the TimeZoneInfo..::.BaseUtcOffset property.

Because the date and time value represented by Time and this value's offset from UTC are not tightly coupled, a local or unspecified date and time value can return a different offset value when run on different computers or when run on the same computer under different time zones. If this behavior is undesirable, use a DateTimeOffset value instead. The DateTimeOffset data type tightly couples a date and time value with its offset from UTC.

The following example uses the GetUtcOffset method to return the Coordinated Universal Time (UTC) offset for several local times.

Visual Basic
' Example of the TimeZone.ToLocalTime( DateTime ) and 
' TimeZone.GetUtcOffset( DateTime ) methods.
Imports System
Imports Microsoft.VisualBasic

Module UTCTimeDemo

    Sub Main( )

        Const headFmt As String = "{0,-20}{1,-20}{2,-12}{3}"

        ' Get the local time zone and a base Coordinated Universal 
        ' Time (UTC).
        Dim localZone As TimeZone = TimeZone.CurrentTimeZone
        Dim baseUTC As DateTime = new DateTime( 2000, 1, 1 )

        Console.WriteLine( "This example of " & vbCrLf & _
            "   TimeZone.ToLocalTime( DateTime ) and" & vbCrLf & _
            "   TimeZone.GetUtcOffset( DateTime ) " & vbCrLf & _
            "generates the following output, which varies " & _
            "depending on the time zone " & vbCrLf & "in which " & _
            "it is run. The example creates several Coordinated " & _
            "Universal " & vbCrLf & "Times (UTC), displays the " & _
            "corresponding local times and UTC offsets, " & _
            vbCrLf & "and shows if the times occur in daylight " & _
            "saving time (DST)." & vbCrLf )
        Console.WriteLine( "Local time: {0}" & vbCrLf, _
            localZone.StandardName )

        Console.WriteLine( headFmt, "UTC", "Local Time", _
            " Offset", "DST?" )
        Console.WriteLine( headFmt, "---", "----------", _
            " ------", "----" )

        ' Generate several UTC times.
        Dim loopX As Integer
        For loopX = 0 to 10

            ' Calculate the local time and UTC offset.
            Dim localTime As DateTime = _
                localZone.ToLocalTime( baseUTC )
            Dim localOffset As TimeSpan = _
                localZone.GetUtcOffset( localTime )

            Console.WriteLine( "{0,-20:yyyy-MM-dd HH:mm}" & _
                "{1,-20:yyyy-MM-dd HH:mm}{2,-12}{3}", _
                baseUTC, localTime, localOffset, _
                localZone.IsDaylightSavingTime( localTime ) )

            ' Advance to another UTC.
            baseUTC = baseUTC.AddDays( 155.55 )
        Next loopX
    End Sub 
End Module 

' This example of
'    TimeZone.ToLocalTime( DateTime ) and
'    TimeZone.GetUtcOffset( DateTime )
' generates the following output, which varies depending on the time zone
' in which it is run. The example creates several Coordinated Universal
' Times (UTC), displays the corresponding local times and UTC offsets,
' and shows if the times occur in daylight saving time (DST).
' 
' Local time: Pacific Standard Time
' 
' UTC                 Local Time           Offset     DST?
' ---                 ----------           ------     ----
' 2000-01-01 00:00    1999-12-31 16:00    -08:00:00   False
' 2000-06-04 13:12    2000-06-04 06:12    -07:00:00   True
' 2000-11-07 02:24    2000-11-06 18:24    -08:00:00   False
' 2001-04-11 15:36    2001-04-11 08:36    -07:00:00   True
' 2001-09-14 04:48    2001-09-13 21:48    -07:00:00   True
' 2002-02-16 18:00    2002-02-16 10:00    -08:00:00   False
' 2002-07-22 07:12    2002-07-22 00:12    -07:00:00   True
' 2002-12-24 20:24    2002-12-24 12:24    -08:00:00   False
' 2003-05-29 09:36    2003-05-29 02:36    -07:00:00   True
' 2003-10-31 22:48    2003-10-31 14:48    -08:00:00   False
' 2004-04-04 12:00    2004-04-04 05:00    -07:00:00   True
C#
// Example of the TimeZone.ToLocalTime( DateTime ) and 
// TimeZone.GetUtcOffset( DateTime ) methods.
using System;

class UTCTimeDemo
{
    static void Main( )
    {
        const string headFmt = "{0,-20}{1,-20}{2,-12}{3}";

        // Get the local time zone and a base Coordinated Universal 
        // Time (UTC).
        TimeZone localZone = TimeZone.CurrentTimeZone;
        DateTime baseUTC = new DateTime( 2000, 1, 1 );

        Console.WriteLine( "This example of \n" +
            "   TimeZone.ToLocalTime( DateTime ) and\n" +
            "   TimeZone.GetUtcOffset( DateTime ) \ngenerates the " +
            "following output, which varies depending on the time " +
            "zone \nin which it is run. The example creates several " +
            "Coordinated Universal \nTimes (UTC), displays the " +
            "corresponding local times and UTC offsets, \nand shows " +
            "if the times occur in daylight saving time (DST)." );
        Console.WriteLine( "\nLocal time: {0}\n", 
            localZone.StandardName );

        Console.WriteLine( headFmt, "UTC", "Local Time", 
            " Offset", "DST?" );
        Console.WriteLine( headFmt, "---", "----------", 
            " ------", "----" );

        // Generate several UTC times.
        for( int loopX = 0; loopX <= 10; loopX++ )
        {
            // Calculate the local time and UTC offset.
            DateTime localTime = localZone.ToLocalTime( baseUTC );
            TimeSpan localOffset = 
                localZone.GetUtcOffset( localTime );

            Console.WriteLine( "{0,-20:yyyy-MM-dd HH:mm}" +
                "{1,-20:yyyy-MM-dd HH:mm}{2,-12}{3}", 
                baseUTC, localTime, localOffset, 
                localZone.IsDaylightSavingTime( localTime ) );

            // Advance to another UTC.
            baseUTC = baseUTC.AddDays( 155.55 );
        }
    } 
} 

/*
This example of
   TimeZone.ToLocalTime( DateTime ) and
   TimeZone.GetUtcOffset( DateTime )
generates the following output, which varies depending on the time zone
in which it is run. The example creates several Coordinated Universal
Times (UTC), displays the corresponding local times and UTC offsets,
and shows if the times occur in daylight saving time (DST).

Local time: Pacific Standard Time

UTC                 Local Time           Offset     DST?
---                 ----------           ------     ----
2000-01-01 00:00    1999-12-31 16:00    -08:00:00   False
2000-06-04 13:12    2000-06-04 06:12    -07:00:00   True
2000-11-07 02:24    2000-11-06 18:24    -08:00:00   False
2001-04-11 15:36    2001-04-11 08:36    -07:00:00   True
2001-09-14 04:48    2001-09-13 21:48    -07:00:00   True
2002-02-16 18:00    2002-02-16 10:00    -08:00:00   False
2002-07-22 07:12    2002-07-22 00:12    -07:00:00   True
2002-12-24 20:24    2002-12-24 12:24    -08:00:00   False
2003-05-29 09:36    2003-05-29 02:36    -07:00:00   True
2003-10-31 22:48    2003-10-31 14:48    -08:00:00   False
2004-04-04 12:00    2004-04-04 05:00    -07:00:00   True
*/ 
Visual C++
// Example of the TimeZone::ToLocalTime( DateTime ) and 
// TimeZone::GetUtcOffset( DateTime ) methods.
using namespace System;
int main()
{
   String^ headFmt = "{0,-20}{1,-20}{2,-12}{3}";

   // Get the local time zone and a base Coordinated Universal 
   // Time (UTC).
   TimeZone^ localZone = TimeZone::CurrentTimeZone;
   DateTime baseUTC = DateTime(2000,1,1);
   Console::WriteLine( "This example of \n"
   "   TimeZone::ToLocalTime( DateTime ) and\n"
   "   TimeZone::GetUtcOffset( DateTime ) \ngenerates the "
   "following output, which varies depending on the time "
   "zone \nin which it is run. The example creates several "
   "Coordinated Universal \nTimes (UTC), displays the "
   "corresponding local times and UTC offsets, \nand shows "
   "if the times occur in daylight saving time (DST)." );
   Console::WriteLine( "\nLocal time: {0}\n", localZone->StandardName );
   Console::WriteLine( headFmt, "UTC", "Local Time", " Offset", "DST?" );
   Console::WriteLine( headFmt, "---", "----------", " ------", "----" );

   // Generate several UTC times.
   for ( int loopX = 0; loopX <= 10; loopX++ )
   {

      // Calculate the local time and UTC offset.
      DateTime localTime = localZone->ToLocalTime( baseUTC );
      TimeSpan localOffset = localZone->GetUtcOffset( localTime );
      Console::WriteLine( "{0,-20:yyyy-MM-dd HH:mm}"
      "{1,-20:yyyy-MM-dd HH:mm}{2,-12}{3}", baseUTC, localTime, localOffset, localZone->IsDaylightSavingTime( localTime ) );

      // Advance to another UTC.
      baseUTC = baseUTC.AddDays( 155.55 );

   }
}

/*
This example of
   TimeZone::ToLocalTime( DateTime ) and
   TimeZone::GetUtcOffset( DateTime )
generates the following output, which varies depending on the time zone
in which it is run. The example creates several Coordinated Universal
Times (UTC), displays the corresponding local times and UTC offsets,
and shows if the times occur in daylight saving time (DST).

Local time: Pacific Standard Time

UTC                 Local Time           Offset     DST?
---                 ----------           ------     ----
2000-01-01 00:00    1999-12-31 16:00    -08:00:00   False
2000-06-04 13:12    2000-06-04 06:12    -07:00:00   True
2000-11-07 02:24    2000-11-06 18:24    -08:00:00   False
2001-04-11 15:36    2001-04-11 08:36    -07:00:00   True
2001-09-14 04:48    2001-09-13 21:48    -07:00:00   True
2002-02-16 18:00    2002-02-16 10:00    -08:00:00   False
2002-07-22 07:12    2002-07-22 00:12    -07:00:00   True
2002-12-24 20:24    2002-12-24 12:24    -08:00:00   False
2003-05-29 09:36    2003-05-29 02:36    -07:00:00   True
2003-10-31 22:48    2003-10-31 14:48    -08:00:00   False
2004-04-04 12:00    2004-04-04 05:00    -07:00:00   True
*/

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
Page view tracker