DateTime.AddYears(Int32) DateTime.AddYears(Int32) DateTime.AddYears(Int32) DateTime.AddYears(Int32) Method

定義

このインスタンスの値に、指定された年数を加算した新しい DateTime を返します。Returns a new DateTime that adds the specified number of years to the value of this instance.

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 Int32 Int32 Int32

年数。A number of years. value パラメーターは、正または負のどちらの場合もあります。The value parameter can be negative or positive.

戻り値

このインスタンスで表された日付と時刻に value で表された年数を加算した値を保持するオブジェクト。An object whose value is the sum of the date and time represented by this instance and the number of years represented by value.

例外

value または結果として返された DateTimeMinValue 未満であるか、MaxValue を超えています。value or the resulting DateTime is less than MinValue or greater than MaxValue.

注釈

このメソッドには、この値は変わりません。DateTimeオブジェクト。This method does not change the value of this DateTime object. 代わりに、新しい返しますDateTime値がこの操作の結果オブジェクト。Instead, it returns a new DateTime object whose value is the result of this operation.

AddYearsメソッドは、うるう年を考慮して、結果の年を計算します。The AddYears method calculates the resulting year taking into account leap years. 結果の月と時刻の部分は、DateTimeオブジェクトがこのインスタンスと同じです。The month and time-of-day part of the resulting DateTime object remains the same as this instance.

現在のインスタンスが閏年で閏日である場合は、戻り値は、対象の日付に依存します。If the current instance represents the leap day in a leap year, the return value depends on the target date:

  • 場合value + DateTime.Year閏年、戻り値はその年の閏日になります。If value + DateTime.Year is also a leap year, the return value represents the leap day in that year. たとえば、4 年が 2012 年 2 月 29 日に追加された場合返される日付が 2016 年 2 月 29 日です。For example, if four years is added to February 29, 2012, the date returned is February 29, 2016.

  • 場合value + DateTime.Yearが閏年で戻り値がその年の閏日の前日を表します。If value + DateTime.Year is not a leap year, the return value represents the day before the leap day in that year. たとえば、1 年が 2012 年 2 月 29 日に追加された場合返される日付は 2013 年 2 月 28 日です。For example, if one year is added to February 29, 2012, the date returned is February 28, 2013.

次の例を使用して、AddYearsメソッドをDateTime閏年日を表す値です。The following example illustrates using the AddYears method with a DateTime value that represents a leap year day. 15 年より前のバージョンと後に続く 15 年の日付を表示しますが、2000 年 2 月 29 日。It displays the date for the fifteen years prior to and the fifteen years that follow February 29, 2000.

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
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

適用対象