Freigeben über


TimeZoneInfo.GetUtcOffset Method (DateTime)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Calculates the offset or difference between the time in this time zone and Coordinated Universal Time (UTC) for a particular date and time.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Function GetUtcOffset ( _
    dateTime As DateTime _
) As TimeSpan
public TimeSpan GetUtcOffset(
    DateTime dateTime
)

Parameters

  • dateTime
    Type: System.DateTime
    The date and time to determine the offset for.

Return Value

Type: System.TimeSpan
A TimeSpan object that indicates the time difference between the two time zones.

Remarks

The returned time span includes any differences due to the application of adjustment rules to the current time zone. It differs from the BaseUtcOffset property, which returns the difference between Coordinated Universal Time (UTC) and the time zone's standard time and, therefore, does not take adjustment rules into account.

If the dateTime parameter's Kind property does not correspond to the time zone object, this method performs the necessary conversion before returning a result. For example, this can occur if the Kind property is DateTimeKind.Local but the time zone object is not the local time zone. If dateTime is ambiguous, or if the converted time is ambiguous, this method interprets the ambiguous time as a standard time. If dateTime is invalid, this method returns a TimeSpan object that reflects the difference between UTC and the time zone's standard time.

Version Notes

XNA Framework

 When this method is used in the XNA Framework, it throws a NotSupportedException exception.

Examples

The following example illustrates the use of the GetUtcOffset(DateTime) method with different time zones and with date values that have different Kind property values.

Option Strict On

Module Example
   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      ShowOffset(outputBlock, #6/12/2006 11:00:00 AM#, TimeZoneInfo.Local)
      ShowOffset(outputBlock, #11/4/2007 1:00:00 AM#, TimeZoneInfo.Local)
      ShowOffset(outputBlock, #12/10/2006 3:00:00 PM#, TimeZoneInfo.Local)
      ShowOffset(outputBlock, Date.UtcNow, TimeZoneInfo.Local)

      ShowOffset(outputBlock, #6/12/2006 11:00:00 AM#, TimeZoneInfo.Utc)
      ShowOffset(outputBlock, #11/4/2007 1:00:00 AM#, TimeZoneInfo.Utc)
      ShowOffset(outputBlock, #12/10/2006 3:00:00 PM#, TimeZoneInfo.Utc)
      ShowOffset(outputBlock, Date.Now, TimeZoneInfo.Utc)
   End Sub

   Private Sub ShowOffset(ByVal outputBlock As System.Windows.Controls.TextBlock, _
                          ByVal time As Date, ByVal timeZone As TimeZoneInfo)
      Dim convertedTime As Date = TimeZoneInfo.ConvertTime(time, timeZone)
      Dim offset As TimeSpan = timeZone.GetUtcOffset(time)

      If time = convertedTime Then
         outputBlock.Text &= String.Format("{0} {1} ", time, _
                           IIf(timeZone.IsDaylightSavingTime(time), _
                               timeZone.DaylightName, _
                               timeZone.StandardName)) & vbCrLf
         outputBlock.Text &= String.Format("   It differs from UTC by {0} hours, {1} minutes.", _
                            offset.Hours, _
                            offset.Minutes) & vbCrLf
      Else
         outputBlock.Text &= String.Format("{0} {1} ", time, _
                           IIf(time.Kind = DateTimeKind.Utc, "UTC", _
                           IIf(TimeZoneInfo.Local.IsDaylightSavingTime(time), _ 
                                 TimeZoneInfo.Local.DaylightName, _
                                 TimeZoneInfo.Local.StandardName))) & vbCrLf
         outputBlock.Text &= String.Format("   converts to {0} {1}.", _
                           convertedTime, _
                           IIf(TimeZoneInfo.Local.IsDaylightSavingTime(time), _ 
                                 TimeZoneInfo.Local.DaylightName, _
                                 TimeZoneInfo.Local.StandardName)) & vbCrLf
         outputBlock.Text &= String.Format("   It differs from UTC by {0} hours, {1} minutes.", _
                           offset.Hours, offset.Minutes) & vbCrLf
      End If
      outputBlock.Text &= vbCrLf
   End Sub
End Module
'
' The example produces the following output:
' 
'       6/12/2006 11:00:00 AM Pacific Daylight Time 
'          It differs from UTC by -7 hours, 0 minutes.
'       
'       11/4/2007 1:00:00 AM Pacific Standard Time 
'          It differs from UTC by -8 hours, 0 minutes.
'       
'       12/10/2006 3:00:00 PM Pacific Standard Time 
'          It differs from UTC by -8 hours, 0 minutes.
'       
'       2/2/2007 8:35:46 PM UTC 
'          converts to 2/2/2007 12:35:46 PM Pacific Standard Time.
'          It differs from UTC by -8 hours, 0 minutes.
'       
'       6/12/2006 11:00:00 AM UTC 
'          It differs from UTC by 0 hours, 0 minutes.
'       
'       11/4/2007 1:00:00 AM UTC 
'          It differs from UTC by 0 hours, 0 minutes.
'       
'       12/10/2006 3:00:00 AM UTC 
'          It differs from UTC by 0 hours, 0 minutes.
'       
'       2/2/2007 12:35:46 PM Pacific Standard Time 
'          converts to 2/2/2007 8:35:46 PM UTC.
'          It differs from UTC by 0 hours, 0 minutes.
using System;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      ShowOffset(outputBlock, new DateTime(2006, 6, 12, 11, 0, 0), TimeZoneInfo.Local);
      ShowOffset(outputBlock, new DateTime(2007, 11, 4, 1, 0, 0), TimeZoneInfo.Local);
      ShowOffset(outputBlock, new DateTime(2006, 12, 10, 15, 0, 0), TimeZoneInfo.Local);
      ShowOffset(outputBlock, DateTime.UtcNow, TimeZoneInfo.Local);

      ShowOffset(outputBlock, new DateTime(2006, 6, 12, 11, 0, 0), TimeZoneInfo.Utc);
      ShowOffset(outputBlock, new DateTime(2007, 11, 4, 1, 0, 0), TimeZoneInfo.Utc);
      ShowOffset(outputBlock, new DateTime(2006, 12, 10, 3, 0, 0), TimeZoneInfo.Utc);
      ShowOffset(outputBlock, DateTime.Now, TimeZoneInfo.Utc);
   }

   private static void ShowOffset(System.Windows.Controls.TextBlock outputBlock, DateTime time, TimeZoneInfo timeZone)
   {
      DateTime convertedTime = TimeZoneInfo.ConvertTime(time, timeZone);
      TimeSpan offset = timeZone.GetUtcOffset(time);

      if (time == convertedTime)
      {
         outputBlock.Text += String.Format("{0} {1} ", time,
                           timeZone.IsDaylightSavingTime(time) ? timeZone.DaylightName : timeZone.StandardName) + "\n";
         outputBlock.Text += String.Format("   It differs from UTC by {0} hours, {1} minutes.",
                            offset.Hours,
                            offset.Minutes) + "\n";
      }
      else
      {
         outputBlock.Text += String.Format("{0} {1} ", time,
                           time.Kind == DateTimeKind.Utc ? "UTC" : 
                           TimeZoneInfo.Local.IsDaylightSavingTime(time) ? 
                                 TimeZoneInfo.Local.DaylightName :
                                 TimeZoneInfo.Local.StandardName) + "\n";
         outputBlock.Text += String.Format("   converts to {0} {1}.",
                           convertedTime,
                           timeZone.IsDaylightSavingTime(convertedTime) ?
                                  timeZone.DaylightName :
                                  timeZone.StandardName) + "\n";
         outputBlock.Text += String.Format("   It differs from UTC by {0} hours, {1} minutes.",
                           offset.Hours, offset.Minutes) + "\n";
      }
      outputBlock.Text += "\n";
   }
}
// The example produces the following output:
//
//       6/12/2006 11:00:00 AM Pacific Daylight Time 
//          It differs from UTC by -7 hours, 0 minutes.
//       
//       11/4/2007 1:00:00 AM Pacific Standard Time 
//          It differs from UTC by -8 hours, 0 minutes.
//       
//       12/10/2006 3:00:00 PM Pacific Standard Time 
//          It differs from UTC by -8 hours, 0 minutes.
//       
//       5/3/2008 11:04:36 PM UTC
//          converts to 5/3/2008 4:04:36 PM UTC. 
//          It differs from UTC by -7 hours, 0 minutes.
//       
//       6/12/2006 11:00:00 AM Pacific Daylight Time
//          converts to 6/12/2006 6:00:00 PM UTC. 
//          It differs from UTC by 0 hours, 0 minutes.
//       
//       11/4/2007 1:00:00 AM Pacific Standard Time
//          converts to 11/4/2007 9:00:00 AM UTC. 
//          It differs from UTC by 0 hours, 0 minutes.
//       
//       12/10/2006 3:00:00 AM Pacific Standard Time
//          converts to 12/10/2006 11:00:00 AM UTC. 
//          It differs from UTC by 0 hours, 0 minutes.
//       
//       5/3/2008 4:04:36 PM Pacific Davlight Time 
//          converts to 5/3/2008 11:04:36 PM UTC.
//          It differs from UTC by 0 hours, 0 minutes.
//       

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.