共用方式為


DateTimeOffset.AddDays(Double) 方法

定義

傳回新的 DateTimeOffset 物件,將這個執行個體的值加上指定的整數和小數天數。

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

參數

days
Double

整數和小數的天數。 可以是負數或正數。

傳回

物件,其值為目前 DateTimeOffset 物件所表示之日期和時間加上 days 所表示之天數的總和。

例外狀況

範例

下列範例會 AddDays 使用 方法來列出 2008 年 3 月工作周的星期一開始的日期。

DateTimeOffset workDay = new DateTimeOffset(2008, 3, 1, 9, 0, 0,
                   DateTimeOffset.Now.Offset);
int month = workDay.Month;
// Start with the first Monday of the month
if (workDay.DayOfWeek != DayOfWeek.Monday)
{
   if (workDay.DayOfWeek == DayOfWeek.Sunday)
      workDay = workDay.AddDays(1);
   else
      workDay = workDay.AddDays(8 - (int)workDay.DayOfWeek);
}
Console.WriteLine("Beginning of Work Week In {0:MMMM} {0:yyyy}:", workDay);
// Add one week to the current date
do
{
   Console.WriteLine("   {0:dddd}, {0:MMMM}{0: d}", workDay);
   workDay = workDay.AddDays(7);
} while (workDay.Month == month);
// The example produces the following output:
//    Beginning of Work Week In March 2008:
//       Monday, March 3
//       Monday, March 10
//       Monday, March 17
//       Monday, March 24
//       Monday, March 31
let workDay = DateTimeOffset(2008, 3, 1, 9, 0, 0, DateTimeOffset.Now.Offset)
let month = workDay.Month

// Start with the first Monday of the month
let mutable workDay = 
    match workDay.DayOfWeek with
    | DayOfWeek.Monday ->
        workDay
    | DayOfWeek.Sunday ->
        workDay.AddDays 1
    | _ ->
        workDay.AddDays(8. - float workDay.DayOfWeek)

printfn $"Beginning of Work Week In {workDay:MMMM} {workDay:yyyy}:"

// Add one week to the current date
while workDay.Month = month do
    printfn $"   {workDay:dddd}, {workDay:MMMM}{workDay: d}"
    workDay <- workDay.AddDays 7

// The example produces the following output:
//    Beginning of Work Week In March 2008:
//       Monday, March 3
//       Monday, March 10
//       Monday, March 17
//       Monday, March 24
//       Monday, March 31
Dim workDay As New DateTimeOffset(#3/1/2008 9:00AM#, _
                   DateTimeOffset.Now.Offset)
Dim month As Integer = workDay.Month
' Start with the first Monday of the month
If workDay.DayOfWeek <> DayOfWeek.Monday Then
   If workDay.DayOfWeek = DayOfWeek.Sunday Then
      workDay = workDay.AddDays(1)
   Else   
      workDay = workDay.AddDays(8 - CInt(workDay.DayOfWeek))
   End If
End If
Console.WriteLine("Beginning of Work Week In {0:MMMM} {0:yyyy}:", workDay)
' Add one week to the current date 
Do While workDay.Month = month
   Console.WriteLine("   {0:dddd}, {0:MMMM}{0: d}", workDay)
   workDay = workDay.AddDays(7)
Loop        
' The example produces the following output:
'    Beginning of Work Week In March 2008:
'       Monday, March 3
'       Monday, March 10
'       Monday, March 17
'       Monday, March 24
'       Monday, March 31

備註

參數的小 days 數部分是一天的分數部分。 例如,4.5 相當於 4 天、12 小時、0 分鐘、0 秒、0 毫秒。

在 .NET 6 和舊版中 days ,參數會四捨五入至最接近的毫秒。 在 .NET 7 和更新版本中,會使用 參數的完整 Doubledays 精確度。 不過,由於浮點數數學的固有不精確性,因此產生的精確度會有所不同。

注意

這個方法會傳回新的 DateTimeOffset 物件。 它不會藉由新增 days 至目前物件的日期和時間來修改其值。

DateTimeOffset因為 物件不代表特定時區中的日期和時間,所以當方法執行日期和時間算術時, AddDays 不會考慮特定時區的調整規則。

將小於一天的時間間隔轉換為分數,可能會牽涉到精確度遺失。 如果這有問題,您可以使用 Add 方法,這可讓您在單一方法呼叫中指定一種以上的時間間隔,並不需要將時間間隔轉換成一天的小數部分。

適用於