This topic has not yet been rated - Rate this topic

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

The TimeZoneInfo.GetUtcOffset(DateTime) method is similar in operation to the GetUtcOffset method of the TimeZone class.

Notes to Callers

On Windows XP systems, this method applies only the current adjustment rule when calculating the offset from UTC if the current instance is TimeZoneInfo.Local. As a result, the method may not accurately calculate the difference between the local time and UTC on dateTime. For more information, see the Notes for Callers section in the Local property.

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;

[assembly:CLSCompliant(true)]
namespace TimeZoneInfoCode
{
   public class TimeOffsets
   {
      public static void Main()
      {
         TimeOffsets timeoff = new TimeOffsets();
         TimeZoneInfo cst = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");

         timeoff.ShowOffset(new DateTime(2006, 6, 12, 11, 0, 0), TimeZoneInfo.Local);
         timeoff.ShowOffset(new DateTime(2007, 11, 4, 1, 0, 0), TimeZoneInfo.Local);
         timeoff.ShowOffset(new DateTime(2006, 12, 10, 15, 0, 0), TimeZoneInfo.Local);
         timeoff.ShowOffset(new DateTime(2007, 3, 11, 2, 30, 0), TimeZoneInfo.Local);
         timeoff.ShowOffset(DateTime.UtcNow, TimeZoneInfo.Local);
         timeoff.ShowOffset(new DateTime(2006, 6, 12, 11, 0, 0), TimeZoneInfo.Utc);
         timeoff.ShowOffset(new DateTime(2007, 11, 4, 1, 0, 0), TimeZoneInfo.Utc);
         timeoff.ShowOffset(new DateTime(2006, 12, 10, 3, 0, 0), TimeZoneInfo.Utc);
         timeoff.ShowOffset(new DateTime(2007, 3, 11, 2, 30, 0), TimeZoneInfo.Utc);
         timeoff.ShowOffset(DateTime.Now, TimeZoneInfo.Utc);
         timeoff.ShowOffset(new DateTime(2006, 6, 12, 11, 0, 0), cst);
         timeoff.ShowOffset(new DateTime(2007, 11, 4, 1, 0, 0), cst);
         timeoff.ShowOffset(new DateTime(2006, 12, 10, 15, 0, 0), cst);
         timeoff.ShowOffset(new DateTime(2007, 3, 11, 2, 30, 0, 0), cst);
         timeoff.ShowOffset(new DateTime(2007, 11, 14, 00, 00, 00, DateTimeKind.Local), cst);
      }

      private void ShowOffset(DateTime time, TimeZoneInfo timeZone)
      {
         DateTime convertedTime = time;  
         TimeSpan offset;

         if (time.Kind == DateTimeKind.Local && ! timeZone.Equals(TimeZoneInfo.Local)) 
            convertedTime = TimeZoneInfo.ConvertTime(time, TimeZoneInfo.Local, timeZone);
         else if (time.Kind == DateTimeKind.Utc && ! timeZone.Equals(TimeZoneInfo.Utc)) 
            convertedTime = TimeZoneInfo.ConvertTime(time, TimeZoneInfo.Utc, timeZone);         

         offset = timeZone.GetUtcOffset(time);
         if (time == convertedTime)
         {
            Console.WriteLine("{0} {1} ", time, 
                              timeZone.IsDaylightSavingTime(time) ? timeZone.DaylightName : timeZone.StandardName);
            Console.WriteLine("   It differs from UTC by {0} hours, {1} minutes.", 
                               offset.Hours, 
                               offset.Minutes);
         }
         else
         {
            Console.WriteLine("{0} {1} ", time, 
                              time.Kind == DateTimeKind.Utc ? "UTC" :  TimeZoneInfo.Local.Id);       
            Console.WriteLine("   converts to {0} {1}.", 
                              convertedTime, 
                              timeZone.Id);
            Console.WriteLine("   It differs from UTC by {0} hours, {1} minutes.", 
                              offset.Hours, offset.Minutes);  
         }
         Console.WriteLine();                                             
      }
   }
}
// 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. 
//        
//       3/11/2007 2:30:00 AM 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. 
//        
//       3/11/2007 2:30: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. 
//        
//       6/12/2006 11:00:00 AM Central Daylight Time  
//          It differs from UTC by -5 hours, 0 minutes. 
//        
//       11/4/2007 1:00:00 AM Central Standard Time  
//          It differs from UTC by -6 hours, 0 minutes. 
//        
//       12/10/2006 3:00:00 PM Central Standard Time  
//          It differs from UTC by -6 hours, 0 minutes. 
//        
//       3/11/2007 2:30:00 AM Central Standard Time  
//          It differs from UTC by -6 hours, 0 minutes. 
//        
//       11/14/2007 12:00:00 AM Pacific Standard Time  
//          converts to 11/14/2007 2:00:00 AM Central Standard Time. 
//          It differs from UTC by -6 hours, 0 minutes.

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.