TaiwanCalendar.AddYears(DateTime, Int32) TaiwanCalendar.AddYears(DateTime, Int32) TaiwanCalendar.AddYears(DateTime, Int32) TaiwanCalendar.AddYears(DateTime, Int32) Method

정의

지정된 DateTime에서 지정된 연도 수만큼 경과한 DateTime을 반환합니다.Returns a DateTime that is the specified number of years away from the specified DateTime.

public:
 override DateTime AddYears(DateTime time, int years);
public override DateTime AddYears (DateTime time, int years);
override this.AddYears : DateTime * int -> DateTime
Public Overrides Function AddYears (time As DateTime, years As Integer) As DateTime

매개 변수

time
DateTime DateTime DateTime DateTime

연도를 더할 DateTime입니다.The DateTime to which to add years.

years
Int32 Int32 Int32 Int32

더할 연도 수입니다.The number of years to add.

반환

지정된 DateTime에 지정된 연도 수를 더한 결과로 만들어지는 DateTime입니다.The DateTime that results from adding the specified number of years to the specified DateTime.

예외

결과 DateTime이 지원되는 범위 밖에 있는 경우The resulting DateTime is outside the supported range.

예제

다음 코드 예제는 사용법을 표시 합니다 AddYears 메서드.The following code example displays the use of the AddYears method.

using namespace System;
using namespace System::Globalization;
void DisplayValues( Calendar^ myCal, DateTime myDT )
{
   Console::WriteLine( "   Era:        {0}", myCal->GetEra( myDT ) );
   Console::WriteLine( "   Year:       {0}", myCal->GetYear( myDT ) );
   Console::WriteLine( "   Month:      {0}", myCal->GetMonth( myDT ) );
   Console::WriteLine( "   DayOfYear:  {0}", myCal->GetDayOfYear( myDT ) );
   Console::WriteLine( "   DayOfMonth: {0}", myCal->GetDayOfMonth( myDT ) );
   Console::WriteLine( "   DayOfWeek:  {0}", myCal->GetDayOfWeek( myDT ) );
   Console::WriteLine();
}

int main()
{
   
   // Sets a DateTime to April 3, 2002 of the Gregorian calendar.
   DateTime myDT = DateTime(2002,4,3,gcnew GregorianCalendar);
   
   // Creates an instance of the TaiwanCalendar.
   TaiwanCalendar^ myCal = gcnew TaiwanCalendar;
   
   // Displays the values of the DateTime.
   Console::WriteLine( "April 3, 2002 of the Gregorian calendar equals the following in the Taiwan calendar:" );
   DisplayValues( myCal, myDT );
   
   // Adds two years and ten months.
   myDT = myCal->AddYears( myDT, 2 );
   myDT = myCal->AddMonths( myDT, 10 );
   
   // Displays the values of the DateTime.
   Console::WriteLine( "After adding two years and ten months:" );
   DisplayValues( myCal, myDT );
}

/*
This code produces the following output.

April 3, 2002 of the Gregorian calendar equals the following in the Taiwan calendar:
   Era:        1
   Year:       91
   Month:      4
   DayOfYear:  93
   DayOfMonth: 3
   DayOfWeek:  Wednesday

After adding two years and ten months:
   Era:        1
   Year:       94
   Month:      2
   DayOfYear:  34
   DayOfMonth: 3
   DayOfWeek:  Thursday

*/
using System;
using System.Globalization;


public class SamplesTaiwanCalendar  {

   public static void Main()  {

      // Sets a DateTime to April 3, 2002 of the Gregorian calendar.
      DateTime myDT = new DateTime( 2002, 4, 3, new GregorianCalendar() );

      // Creates an instance of the TaiwanCalendar.
      TaiwanCalendar myCal = new TaiwanCalendar();

      // Displays the values of the DateTime.
      Console.WriteLine( "April 3, 2002 of the Gregorian calendar equals the following in the Taiwan calendar:" );
      DisplayValues( myCal, myDT );

      // Adds two years and ten months.
      myDT = myCal.AddYears( myDT, 2 );
      myDT = myCal.AddMonths( myDT, 10 );

      // Displays the values of the DateTime.
      Console.WriteLine( "After adding two years and ten months:" );
      DisplayValues( myCal, myDT );

   }

   public static void DisplayValues( Calendar myCal, DateTime myDT )  {
      Console.WriteLine( "   Era:        {0}", myCal.GetEra( myDT ) );
      Console.WriteLine( "   Year:       {0}", myCal.GetYear( myDT ) );
      Console.WriteLine( "   Month:      {0}", myCal.GetMonth( myDT ) );
      Console.WriteLine( "   DayOfYear:  {0}", myCal.GetDayOfYear( myDT ) );
      Console.WriteLine( "   DayOfMonth: {0}", myCal.GetDayOfMonth( myDT ) );
      Console.WriteLine( "   DayOfWeek:  {0}", myCal.GetDayOfWeek( myDT ) );
      Console.WriteLine();
   }

}

/*
This code produces the following output.

April 3, 2002 of the Gregorian calendar equals the following in the Taiwan calendar:
   Era:        1
   Year:       91
   Month:      4
   DayOfYear:  93
   DayOfMonth: 3
   DayOfWeek:  Wednesday

After adding two years and ten months:
   Era:        1
   Year:       94
   Month:      2
   DayOfYear:  34
   DayOfMonth: 3
   DayOfWeek:  Thursday

*/
Imports System.Globalization


Public Class SamplesTaiwanCalendar   

   Public Shared Sub Main()

      ' Sets a DateTime to April 3, 2002 of the Gregorian calendar.
      Dim myDT As New DateTime(2002, 4, 3, New GregorianCalendar())

      ' Creates an instance of the TaiwanCalendar.
      Dim myCal As New TaiwanCalendar()

      ' Displays the values of the DateTime.
      Console.WriteLine("April 3, 2002 of the Gregorian calendar equals the following in the Taiwan calendar:")
      DisplayValues(myCal, myDT)

      ' Adds two years and ten months.
      myDT = myCal.AddYears(myDT, 2)
      myDT = myCal.AddMonths(myDT, 10)

      ' Displays the values of the DateTime.
      Console.WriteLine("After adding two years and ten months:")
      DisplayValues(myCal, myDT)

   End Sub

   Public Shared Sub DisplayValues(myCal As Calendar, myDT As DateTime)
      Console.WriteLine("   Era:        {0}", myCal.GetEra(myDT))
      Console.WriteLine("   Year:       {0}", myCal.GetYear(myDT))
      Console.WriteLine("   Month:      {0}", myCal.GetMonth(myDT))
      Console.WriteLine("   DayOfYear:  {0}", myCal.GetDayOfYear(myDT))
      Console.WriteLine("   DayOfMonth: {0}", myCal.GetDayOfMonth(myDT))
      Console.WriteLine("   DayOfWeek:  {0}", myCal.GetDayOfWeek(myDT))
      Console.WriteLine()
   End Sub

End Class 'SamplesTaiwanCalendar 


'This code produces the following output.

'

'April 3, 2002 of the Gregorian calendar equals the following in the Taiwan calendar:

'   Era:        1

'   Year:       91

'   Month:      4

'   DayOfYear:  93

'   DayOfMonth: 3

'   DayOfWeek:  Wednesday

'

'After adding two years and ten months:

'   Era:        1

'   Year:       94

'   Month:      2

'   DayOfYear:  34

'   DayOfMonth: 3

'   DayOfWeek:  Thursday


설명

결과의 일 부분 DateTime 결과 일 결과 결과 연도 월에 유효한 날짜가 아닌 경우 영향을 받습니다.The day part of the resulting DateTime is affected if the resulting day is not a valid day in the resulting month of the resulting year. 결과 결과 연도 월의 마지막 유효 날짜로 변경 됩니다.It is changed to the last valid day in the resulting month of the resulting year. 결과의 월 부분 DateTime 동일 하 게 유지 된 DateTime합니다.The month part of the resulting DateTime remains the same as the specified DateTime. 이 구현은 현재 연대만 지원합니다.This implementation supports only the current era. 따라서 ArgumentException 결과인 연도 지정 된 연대를 벗어나는 경우 throw 됩니다 DateTime합니다.Therefore, ArgumentException is thrown if the resulting year is outside the era of the specified DateTime. 결과 시간 부분은 DateTime 동일 하 게 유지 된 DateTime합니다.The time-of-day part of the resulting DateTime remains the same as the specified DateTime.

예를 들어, 2 월에 28 일을 제외 하 고 29 일까 지 했을 때 윤년For example, February has 28 days, except during leap years when it has 29 days. 지정된 된 날짜 값과 윤년의 2 월 29 일 경우 years 이 1 이면 결과 날짜가 다음 년에서 2 월 28 일 합니다.If the specified date is the 29th day of February in a leap year and the value of years is 1, the resulting date is the 28th day of February in the following year.

하는 경우 years 음수인 경우 결과 DateTime 지정 된 이전의 DateTime합니다.If years is negative, the resulting DateTime is earlier than the specified DateTime.

합니다 Kind 반환 된 속성 DateTime 값이 항상 DateTimeKind.Unspecified합니다.The Kind property of the returned DateTime value always equals DateTimeKind.Unspecified. 유지할 수 있습니다는 Kind 의 속성을 time 호출 하 여 매개 변수는 DateTime.SpecifyKind 메서드를 다음 예제와 같이 합니다.You can preserve the Kind property of the time parameter by calling the DateTime.SpecifyKind method, as the following example shows.

returnTime = DateTime.SpecifyKind(cal.AddYears(time, years), time.Kind);
returnTime = DateTime.SpecifyKind(cal.AddYears(time, years), time.Kind)

적용 대상

추가 정보