DateTime.AddDays(Double) 方法

定義

傳回新的 DateTime,將這個執行個體的值加上指定的天數。

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

參數

value
Double

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

傳回

物件,其值為這個執行個體所表示日期和時間加上 value 所表示天數的總和。

例外狀況

範例

下列範例會 AddDays 使用 方法來判斷目前日期之後第 36 天的星期幾。

using namespace System;

int main()
{
   // Calculate what day of the week is 36 days from this instant.
   DateTime today = System::DateTime::Now;
   DateTime answer = today.AddDays( 36 );
   Console::WriteLine("Today: {0:dddd}", today);
   Console::WriteLine("36 days from today: {0:dddd}", answer);
}
// The example displays output like the following:
//       Today: Wednesday
//       36 days from today: Thursday
open System

let today = DateTime.Now
let answer = today.AddDays 36
printfn $"Today: {today:dddd}"
printfn $"36 days from today: {answer:dddd}"


// The example displays output like the following:
//       Today: Wednesday
//       36 days from today: Thursday
using System;

class Class1
{
    static void Main()
    {
        DateTime today = DateTime.Now;
        DateTime answer = today.AddDays(36);
        Console.WriteLine("Today: {0:dddd}", today);
        Console.WriteLine("36 days from today: {0:dddd}", answer);
    }
}
// The example displays output like the following:
//       Today: Wednesday
//       36 days from today: Thursday
Class Class1
   Public Shared Sub Main()
      Dim today As System.DateTime
      Dim answer As System.DateTime

      today = System.DateTime.Now
      answer = today.AddDays(36)

      Console.WriteLine("Today: {0:dddd}", today)
      Console.WriteLine("36 days from today: {0:dddd}", answer)
   End Sub
End Class
' The example displays output like the following:
'       Today: Wednesday
'       36 days from today: Thursday

備註

這個方法不會變更這個 DateTime 的值。 相反地,它會傳回新的 DateTime ,其值為此作業的結果。

的小 value 數部分是一天的分數部分。 例如,4.5 相當於 4 天、12 小時、0 分鐘、0 秒、0 毫秒和 0 個刻度。

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

方法 AddDays 會考慮閏年,以及執行日期算術時,一個月中的天數。

適用於

另請參閱