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 也是闰年,则返回值表示该年的闰日。 例如,如果将 4 年添加到 2012 年 2 月 29 日,则返回的日期为 2016 年 2 月 29 日。

  • 如果 value + DateTime.Year 不是闰年,则返回值表示该年闰日的前一天。 例如,如果将一年添加到 2012 年 2 月 29 日,则返回的日期为 2013 年 2 月 28 日。

以下示例演示如何将 方法与DateTime表示闰年一天的 值结合使用AddYears。 它显示 2000 年 2 月 29 日之前的 15 年和 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

适用于