共用方式為


DateTimeOffset.AddHours(Double) 方法

定義

回傳一個新 DateTimeOffset 物件,將指定數量的整小時與分數小時加入該實例值。

public:
 DateTimeOffset AddHours(double hours);
public DateTimeOffset AddHours(double hours);
member this.AddHours : double -> DateTimeOffset
Public Function AddHours (hours As Double) As DateTimeOffset

參數

hours
Double

總時數和分數小時。 這個數字可以是負數或正數。

傳回

一個物件,其值為當前 DateTimeOffset 物件所代表的日期與時間與所 hours代表的工時數之和。

例外狀況

範例

以下範例使用該 AddHours 方法,列出某一週辦公室每天兩個八小時班次的開始時間。

const int SHIFT_LENGTH = 8;

DateTimeOffset startTime = new DateTimeOffset(2007, 8, 6, 0, 0, 0,
                     DateTimeOffset.Now.Offset);
DateTimeOffset startOfShift = startTime.AddHours(SHIFT_LENGTH);

Console.WriteLine("Shifts for the week of {0:D}", startOfShift);
do
{
   // Exclude third shift
   if (startOfShift.Hour > 6)
      Console.WriteLine("   {0:d} at {0:T}", startOfShift);

   startOfShift = startOfShift.AddHours(SHIFT_LENGTH);
} while (startOfShift.DayOfWeek != DayOfWeek.Saturday &
           startOfShift.DayOfWeek != DayOfWeek.Sunday);
// The example produces the following output:
//
//    Shifts for the week of Monday, August 06, 2007
//       8/6/2007 at 8:00:00 AM
//       8/6/2007 at 4:00:00 PM
//       8/7/2007 at 8:00:00 AM
//       8/7/2007 at 4:00:00 PM
//       8/8/2007 at 8:00:00 AM
//       8/8/2007 at 4:00:00 PM
//       8/9/2007 at 8:00:00 AM
//       8/9/2007 at 4:00:00 PM
//       8/10/2007 at 8:00:00 AM
//       8/10/2007 at 4:00:00 PM
let shiftLength = 8

let startTime = DateTimeOffset(2007, 8, 6, 0, 0, 0, DateTimeOffset.Now.Offset)
let mutable startOfShift = startTime.AddHours shiftLength

printfn $"Shifts for the week of {startOfShift:D}"
while startOfShift.DayOfWeek <> DayOfWeek.Saturday &&
      startOfShift.DayOfWeek <> DayOfWeek.Sunday do
    // Exclude third shift
    if startOfShift.Hour > 6 then
        printfn $"   {startOfShift:d} at {startOfShift:T}"

    startOfShift <- startOfShift.AddHours shiftLength

// The example produces the following output:
//
//    Shifts for the week of Monday, August 06, 2007
//       8/6/2007 at 8:00:00 AM
//       8/6/2007 at 4:00:00 PM
//       8/7/2007 at 8:00:00 AM
//       8/7/2007 at 4:00:00 PM
//       8/8/2007 at 8:00:00 AM
//       8/8/2007 at 4:00:00 PM
//       8/9/2007 at 8:00:00 AM
//       8/9/2007 at 4:00:00 PM
//       8/10/2007 at 8:00:00 AM
//       8/10/2007 at 4:00:00 PM
Const SHIFT_LENGTH As Integer = 8

Dim startTime As New DateTimeOffset(#8/6/2007 12:00:00AM#, _
                     DateTimeOffset.Now.Offset)
Dim startOfShift As DateTimeOffset = startTime.AddHours(SHIFT_LENGTH)

Console.WriteLine("Shifts for the week of {0:D}", startOfShift)                           
Do 
   ' Exclude third shift
   If startOfShift.Hour > 6 Then _
      Console.WriteLine("   {0:d} at {0:T}", startOfShift)

   startOfShift = startOfShift.AddHours(SHIFT_LENGTH)
Loop While startOfShift.DayOfWeek <> DayOfWeek.Saturday And _
           startOfShift.DayOfWeek <> DayOfWeek.Sunday
          
' The example produces the following output:
'
'    Shifts for the week of Monday, August 06, 2007
'       8/6/2007 at 8:00:00 AM
'       8/6/2007 at 4:00:00 PM
'       8/7/2007 at 8:00:00 AM
'       8/7/2007 at 4:00:00 PM
'       8/8/2007 at 8:00:00 AM
'       8/8/2007 at 4:00:00 PM
'       8/9/2007 at 8:00:00 AM
'       8/9/2007 at 4:00:00 PM
'       8/10/2007 at 8:00:00 AM
'       8/10/2007 at 4:00:00 PM

備註

參數的 hours 分數部分是小時的分數部分。 例如,4.5 相當於 4 小時 30 分鐘 0 秒 0 毫秒。

在 .NET Framework 中,參數 hours 會四捨五入到最接近的毫秒。 在 .NET 7 及之後版本中,參數的精確度會被使用。Doublehours 然而,由於浮點數學本身的不精確性,所得精度會有所不同。

備註

此方法回傳一個新 DateTimeOffset 物件。 它不會透過增加 hours 日期和時間來改變當前物件的值。

由於物件 DateTimeOffset 不代表特定時區的日期與時間, AddHours 該方法在執行日期與時間運算時,不會考慮該時區的調整規則。

將少於一小時的時間間隔轉換為分數,可能會損失精確度。 (例如,一分鐘等於一小時的0.01666。)如果這有問題,你可以使用這個 Add 方法,它能在單一方法呼叫中指定多種時間區間,省去將時間區間轉換成小時的分數。

適用於

另請參閱