TimeZoneInfo.GetUtcOffset 方法

定义

计算此时区中的时间与协调世界时 (UTC) 之间针对特定日期和时间的偏移量或差值。

重载

GetUtcOffset(DateTime)

计算此时区中的时间与协调世界时 (UTC) 之间针对特定日期和时间的偏移量或差值。

GetUtcOffset(DateTimeOffset)

计算此时区中的时间与协调世界时 (UTC) 之间针对特定日期和时间的偏移量或差值。

GetUtcOffset(DateTime)

Source:
TimeZoneInfo.cs
Source:
TimeZoneInfo.cs
Source:
TimeZoneInfo.cs

计算此时区中的时间与协调世界时 (UTC) 之间针对特定日期和时间的偏移量或差值。

public:
 TimeSpan GetUtcOffset(DateTime dateTime);
public TimeSpan GetUtcOffset (DateTime dateTime);
member this.GetUtcOffset : DateTime -> TimeSpan
Public Function GetUtcOffset (dateTime As DateTime) As TimeSpan

参数

dateTime
DateTime

要为其确定偏移量的日期和时间。

返回

一个对象,该对象指示两个时区之间的时差。

示例

以下示例演示了对不同时区和具有不同Kind属性值的GetUtcOffset(DateTime)日期值的方法的用法。

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.
open System

let showOffset (time: DateTime) (timeZone: TimeZoneInfo) =
    let convertedTime = 
        match time.Kind with
        | DateTimeKind.Local when not (timeZone.Equals TimeZoneInfo.Local) ->
            TimeZoneInfo.ConvertTime(time, TimeZoneInfo.Local, timeZone)
        | DateTimeKind.Utc when not (timeZone.Equals TimeZoneInfo.Utc) -> 
            TimeZoneInfo.ConvertTime(time, TimeZoneInfo.Utc, timeZone)         
        | _ -> time

    let offset = timeZone.GetUtcOffset time
    if time = convertedTime then
        printfn $"{time} {if timeZone.IsDaylightSavingTime time then timeZone.DaylightName else timeZone.StandardName} "
        printfn $"   It differs from UTC by {offset.Hours} hours, {offset.Minutes} minutes."
    else
        printfn $"""{time} {if time.Kind = DateTimeKind.Utc then "UTC" else TimeZoneInfo.Local.Id} """       
        printfn $"   converts to {convertedTime} {timeZone.Id}." 
        printfn $"   It differs from UTC by {offset.Hours} hours, {offset.Minutes} minutes."  
    printfn ""                                             

let cst = TimeZoneInfo.FindSystemTimeZoneById "Central Standard Time"

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

// 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.
Option Strict On

<Assembly: CLSCompliant(True)>
Module TimeOffsets
   Public Sub Main()
      Dim cst As TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time")

      ShowOffset(#6/12/2006 11:00AM#, TimeZoneInfo.Local)
      ShowOffset(#11/4/2007 1:00AM#, TimeZoneInfo.Local)
      ShowOffset(#12/10/2006 3:00PM#, TimeZoneInfo.Local)
      ShowOffset(#3/11/2007 2:30:00AM#, TimeZoneInfo.Local)
      ShowOffset(Date.UtcNow, TimeZoneInfo.Local)
      ShowOffset(#6/12/2006 11:00AM#, TimeZoneInfo.Utc)
      ShowOffset(#11/4/2007 1:00AM#, TimeZoneInfo.Utc)
      ShowOffset(#12/10/2006 3:00PM#, TimeZoneInfo.Utc)
      ShowOffset(#3/11/2007 2:30:00AM#, TimeZoneInfo.Utc)
      ShowOffset(Date.Now, TimeZoneInfo.Utc)
      ShowOffset(#6/12/2006 11:00AM#, cst)
      ShowOffset(#11/4/2007 1:00AM#, cst)
      ShowOffset(#12/10/2006 3:00PM#, cst)
      ShowOffset(#3/11/2007 2:30:00AM#, cst)
      ShowOffset(New Date(2007, 11, 14, 00, 00, 00, DateTimeKind.Local), cst)
   End Sub
   
   Private Sub ShowOffset(time As Date, timeZone As TimeZoneInfo)
      Dim convertedTime As Date = time  
      Dim offset As TimeSpan
      
      If time.Kind = DateTimeKind.Local And Not timeZone.Equals(TimeZoneInfo.Local) Then
         convertedTime = TimeZoneInfo.ConvertTime(time, TimeZoneInfo.Local, timeZone)
      ElseIf time.Kind = DateTimeKind.Utc And Not timeZone.Equals(TimeZoneInfo.Utc) Then
         convertedTime = TimeZoneInfo.ConvertTime(time, TimeZoneInfo.Utc, timeZone)         
      End If
      offset = timeZone.GetUtcOffset(time)
      If time = convertedTime Then
         Console.WriteLine("{0} {1} ", time, _
                           IIf(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, _
                           IIf(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)  
       End If
       Console.WriteLine()                                             
   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.
'       
'       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.

注解

由于将调整规则应用于当前时区,返回的时间跨度包括任何差异。 它不同于 BaseUtcOffset 属性,后者返回协调世界时 (UTC) 与时区的标准时间之间的差值,因此不考虑调整规则。

dateTime如果参数的 Kind 属性不对应于时区对象,则此方法在返回结果之前执行必要的转换。 例如,如果 Kind 属性为 DateTimeKind.Local ,但时区对象不是本地时区,则可能会发生这种情况。 如果 dateTime 不明确,或者转换的时间不明确,则此方法将模棱两可的时间解释为标准时间。 如果 dateTime 无效,此方法返回一个 TimeSpan 对象,该对象反映 UTC 与时区的标准时间之间的差异。

方法 TimeZoneInfo.GetUtcOffset(DateTime) 在操作 GetUtcOffset 上类似于 类的 TimeZone 方法。

另请参阅

适用于

GetUtcOffset(DateTimeOffset)

Source:
TimeZoneInfo.cs
Source:
TimeZoneInfo.cs
Source:
TimeZoneInfo.cs

计算此时区中的时间与协调世界时 (UTC) 之间针对特定日期和时间的偏移量或差值。

public:
 TimeSpan GetUtcOffset(DateTimeOffset dateTimeOffset);
public TimeSpan GetUtcOffset (DateTimeOffset dateTimeOffset);
member this.GetUtcOffset : DateTimeOffset -> TimeSpan
Public Function GetUtcOffset (dateTimeOffset As DateTimeOffset) As TimeSpan

参数

dateTimeOffset
DateTimeOffset

要为其确定偏移量的日期和时间。

返回

一个对象,该对象指示协调世界时 (UTC) 与当前时区之间的时差。

注解

由于将调整规则应用于当前时区,返回的时间跨度包括任何差异。 它不同于 BaseUtcOffset 属性,后者返回协调世界时 (UTC) 与时区的标准时间之间的差值,因此不考虑调整规则。

适用于