Export (0) Print
Expand All

TimeZoneInfo.GetUtcOffset Method (DateTime)

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)

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.

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.

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.


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.
//       


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

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

Community Additions

ADD
Show:
© 2014 Microsoft