共用方式為


DateTime.AddYears(Int32) 方法

定義

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

public:
 DateTime AddYears(int value);
public DateTime AddYears (int value);
member this.AddYears : int -> DateTime
Public Function AddYears (value As Integer) As DateTime

參數

value
Int32

年份數。 value 參數可以是負數或正數。

傳回

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

例外狀況

備註

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

方法 AddYears 會計算產生的年份,並考慮閏年。 結果 DateTime 物件的月份和當日部分會維持與這個實例相同。

如果目前的實例代表閏年的閏日,則傳回值取決於目標日期:

  • 如果 value + DateTime.Year 也是閏年,則傳回值代表該年份中的閏日。 例如,如果四年新增至 2012 年 2 月 29 日,則傳回的日期為 2016 年 2 月 29 日。

  • 如果 value + DateTime.Year 不是閏年,則傳回值代表該年份中閏日之前的日期。 例如,如果一年新增至 2012 年 2 月 29 日,則傳回的日期為 2013 年 2 月 28 日。

下列範例說明如何使用 AddYears 方法搭配 DateTime 代表閏年日的值。 它會顯示 2000 年 2 月 29 日之前 15 年的日期,以及 2000 年 2 月 29 日之後的 15 年。

using System;

public class Example
{
   public static void Main()
   {
      DateTime baseDate = new DateTime(2000, 2, 29);
      Console.WriteLine("    Base Date:        {0:d}\n", baseDate);

      // Show dates of previous fifteen years.
      for (int ctr = -1; ctr >= -15; ctr--)
         Console.WriteLine("{0,2} year(s) ago:        {1:d}",
                           Math.Abs(ctr), baseDate.AddYears(ctr));
      Console.WriteLine();

      // Show dates of next fifteen years.
      for (int ctr = 1; ctr <= 15; ctr++)
         Console.WriteLine("{0,2} year(s) from now:   {1:d}",
                           ctr, baseDate.AddYears(ctr));
   }
}
// The example displays the following output:
//           Base Date:        2/29/2000
//
//        1 year(s) ago:        2/28/1999
//        2 year(s) ago:        2/28/1998
//        3 year(s) ago:        2/28/1997
//        4 year(s) ago:        2/29/1996
//        5 year(s) ago:        2/28/1995
//        6 year(s) ago:        2/28/1994
//        7 year(s) ago:        2/28/1993
//        8 year(s) ago:        2/29/1992
//        9 year(s) ago:        2/28/1991
//       10 year(s) ago:        2/28/1990
//       11 year(s) ago:        2/28/1989
//       12 year(s) ago:        2/29/1988
//       13 year(s) ago:        2/28/1987
//       14 year(s) ago:        2/28/1986
//       15 year(s) ago:        2/28/1985
//
//        1 year(s) from now:   2/28/2001
//        2 year(s) from now:   2/28/2002
//        3 year(s) from now:   2/28/2003
//        4 year(s) from now:   2/29/2004
//        5 year(s) from now:   2/28/2005
//        6 year(s) from now:   2/28/2006
//        7 year(s) from now:   2/28/2007
//        8 year(s) from now:   2/29/2008
//        9 year(s) from now:   2/28/2009
//       10 year(s) from now:   2/28/2010
//       11 year(s) from now:   2/28/2011
//       12 year(s) from now:   2/29/2012
//       13 year(s) from now:   2/28/2013
//       14 year(s) from now:   2/28/2014
//       15 year(s) from now:   2/28/2015
open System

let baseDate = DateTime(2000, 2, 29)
printfn $"    Base Date:        {baseDate:d}\n"

// Show dates of previous fifteen years.
for i = -1 downto -15 do
    printfn $"{-i,2} year(s) ago:        {baseDate.AddYears i:d}"
printfn ""

// Show dates of next fifteen years.
for i = 1 to 15 do
    printfn $"{i,2} year(s) from now:   {baseDate.AddYears i:d}"


// The example displays the following output:
//           Base Date:        2/29/2000
//
//        1 year(s) ago:        2/28/1999
//        2 year(s) ago:        2/28/1998
//        3 year(s) ago:        2/28/1997
//        4 year(s) ago:        2/29/1996
//        5 year(s) ago:        2/28/1995
//        6 year(s) ago:        2/28/1994
//        7 year(s) ago:        2/28/1993
//        8 year(s) ago:        2/29/1992
//        9 year(s) ago:        2/28/1991
//       10 year(s) ago:        2/28/1990
//       11 year(s) ago:        2/28/1989
//       12 year(s) ago:        2/29/1988
//       13 year(s) ago:        2/28/1987
//       14 year(s) ago:        2/28/1986
//       15 year(s) ago:        2/28/1985
//
//        1 year(s) from now:   2/28/2001
//        2 year(s) from now:   2/28/2002
//        3 year(s) from now:   2/28/2003
//        4 year(s) from now:   2/29/2004
//        5 year(s) from now:   2/28/2005
//        6 year(s) from now:   2/28/2006
//        7 year(s) from now:   2/28/2007
//        8 year(s) from now:   2/29/2008
//        9 year(s) from now:   2/28/2009
//       10 year(s) from now:   2/28/2010
//       11 year(s) from now:   2/28/2011
//       12 year(s) from now:   2/29/2012
//       13 year(s) from now:   2/28/2013
//       14 year(s) from now:   2/28/2014
//       15 year(s) from now:   2/28/2015
Module Example
   Public Sub Main()
      Dim baseDate As Date = #2/29/2000#
      Console.WriteLine("    Base Date:        {0:d}", baseDate)
      Console.WriteLine()
      
      ' Show dates of previous fifteen years.
      For ctr As Integer = -1 To -15 Step -1
         Console.WriteLine("{0,3} years ago:        {1:d}", 
                           ctr, baseDate.AddYears(ctr))
      Next
      Console.WriteLine()
      ' Show dates of next fifteen years.
      For ctr As Integer = 1 To 15
         Console.WriteLine("{0,3} years from now:   {1:d}", 
                           ctr, baseDate.AddYears(ctr))
      Next      
   End Sub
End Module
' The example displays the following output:
'           Base Date:        2/29/2000
'       
'        1 year(s) ago:        2/28/1999
'        2 year(s) ago:        2/28/1998
'        3 year(s) ago:        2/28/1997
'        4 year(s) ago:        2/29/1996
'        5 year(s) ago:        2/28/1995
'        6 year(s) ago:        2/28/1994
'        7 year(s) ago:        2/28/1993
'        8 year(s) ago:        2/29/1992
'        9 year(s) ago:        2/28/1991
'       10 year(s) ago:        2/28/1990
'       11 year(s) ago:        2/28/1989
'       12 year(s) ago:        2/29/1988
'       13 year(s) ago:        2/28/1987
'       14 year(s) ago:        2/28/1986
'       15 year(s) ago:        2/28/1985
'       
'        1 year(s) from now:   2/28/2001
'        2 year(s) from now:   2/28/2002
'        3 year(s) from now:   2/28/2003
'        4 year(s) from now:   2/29/2004
'        5 year(s) from now:   2/28/2005
'        6 year(s) from now:   2/28/2006
'        7 year(s) from now:   2/28/2007
'        8 year(s) from now:   2/29/2008
'        9 year(s) from now:   2/28/2009
'       10 year(s) from now:   2/28/2010
'       11 year(s) from now:   2/28/2011
'       12 year(s) from now:   2/29/2012
'       13 year(s) from now:   2/28/2013
'       14 year(s) from now:   2/28/2014
'       15 year(s) from now:   2/28/2015

適用於