TaiwanCalendar.AddMonths(DateTime, Int32) Method

Definition

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

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

Parameters

time
DateTime

월을 더할 DateTime입니다.The DateTime to which to add months.

months
Int32

추가할 개월 수입니다.The number of months to add.

Returns

DateTime

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

Exceptions

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

months가 -120000보다 작습니다.months is less than -120000.

-또는--or- months가 120000보다 큽니다.months is greater than 120000.

Examples

다음 코드 예제에서는 AddMonths 메서드.The following code example demonstrates the use of the AddMonths 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


'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


Remarks

결과 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 year part of the resulting DateTime is affected if the resulting month is outside the year of the specified DateTime. 이 구현은 현재 연대만 지원합니다.This implementation supports only the current era. 따라서 결과 연도가 지정 된 DateTime연대를 벗어나면 ArgumentException throw 됩니다.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.

예를 들어, 지정 된 월이 31 일을 포함 하는 10 월이 months 고, 지정 된 일은 해당 월의 31 일이 고, 결과 연도는 6 월이 고, 결과 연도는 4 월이 고, 결과 일은 4 월의 마지막 날 인 30 일입니다.For example, if the specified month is October, which has 31 days, the specified day is the 31st day of that month, and the value of the months parameter is 6, the resulting year is one more than the specified year, the resulting month is April, and the resulting day is the 30th day, which is the last day in April.

months 매개 변수의 값이 음수 이면 결과 DateTime 지정 된 DateTime보다 빠릅니다.If the value of the months parameter is negative, the resulting DateTime is earlier than the specified DateTime.

반환 된 DateTime 값의 Kind 속성은 항상 DateTimeKind.Unspecified와 같습니다.The Kind property of the returned DateTime value always equals DateTimeKind.Unspecified. 다음 예제와 같이 DateTime.SpecifyKind 메서드를 호출 하 여 time 매개 변수의 Kind 속성을 유지할 수 있습니다.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.AddMonths(time, months), time.Kind);
returnTime = DateTime.SpecifyKind(cal.AddMonths(time, months), time.Kind)

Applies to

See also