JapaneseCalendar.AddMonths(DateTime, Int32) JapaneseCalendar.AddMonths(DateTime, Int32) JapaneseCalendar.AddMonths(DateTime, Int32) JapaneseCalendar.AddMonths(DateTime, Int32) Method

定義

指定した 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

パラメーター

time
DateTime DateTime DateTime DateTime

月数を加算する対象の DateTimeThe DateTime to which to add months.

months
Int32 Int32 Int32 Int32

加算する月数。The number of months to add.

戻り値

指定した DateTime に指定した月数を加算した結果の DateTimeThe DateTime that results from adding the specified number of months to the specified DateTime.

例外

結果として得られる DateTime がサポートされている範囲外の値です。The resulting DateTime is outside the supported range.

months が -120000 未満です。months is less than -120000.

または-or- months が 120000 を超えています。months is greater than 120000.

次のコード例のいくつかのコンポーネントの値を表示する、DateTime和暦の観点から。The following code example displays the values of several components of a DateTime in terms of the Japanese calendar.

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 JapaneseCalendar.
   JapaneseCalendar^ myCal = gcnew JapaneseCalendar;
   
   // Displays the values of the DateTime.
   Console::WriteLine( "April 3, 2002 of the Gregorian calendar equals the following in the Japanese 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 Japanese calendar:
   Era:        4
   Year:       14
   Month:      4
   DayOfYear:  93
   DayOfMonth: 3
   DayOfWeek:  Wednesday

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

*/
using System;
using System.Globalization;


public class SamplesJapaneseCalendar  {

   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 JapaneseCalendar.
      JapaneseCalendar myCal = new JapaneseCalendar();

      // Displays the values of the DateTime.
      Console.WriteLine( "April 3, 2002 of the Gregorian calendar equals the following in the Japanese 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 Japanese calendar:
   Era:        4
   Year:       14
   Month:      4
   DayOfYear:  93
   DayOfMonth: 3
   DayOfWeek:  Wednesday

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

*/
Imports System
Imports System.Globalization


Public Class SamplesJapaneseCalendar   

   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 JapaneseCalendar.
      Dim myCal As New JapaneseCalendar()

      ' Displays the values of the DateTime.
      Console.WriteLine("April 3, 2002 of the Gregorian calendar equals the following in the Japanese 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 'Main

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

End Class 'SamplesJapaneseCalendar 


'This code produces the following output.

'

'April 3, 2002 of the Gregorian calendar equals the following in the Japanese calendar:
'   Era:        4
'   Year:       14
'   Month:      4
'   DayOfYear:  93
'   DayOfMonth: 3
'   DayOfWeek:  Wednesday
'
'After adding two years and ten months:
'   Era:        4
'   Year:       17
'   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 year part of the resulting DateTime is affected if the resulting month is outside the year of the specified DateTime. 結果の時代 (年号) の一部DateTime結果の年が指定した時代 (年号) を超えた場合は、影響を受けるDateTimeします。The era part of the resulting DateTime is affected 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 日に Juugatsu (年 10 月) の場合に、指定した日が、その月の 31 日とmonths6 は、結果の年は、1 つ Shigatsu (4 月)、および結果として得られる日、月は、指定した年を超える最後の日 Shigatsu (年 4 月) である 30 日です。For example, if the specified month is Juugatsu (October), which has 31 days, the specified day is the 31st day of that month, and months is 6, the resulting year is one more than the specified year, the resulting month is Shigatsu (April), and the resulting day is the 30th day, which is the last day in Shigatsu (April).

場合monthsが負の場合、その結果DateTimeが、指定よりも前DateTimeします。If months is negative, the resulting DateTime is earlier than the specified DateTime.

Kind 、返されたプロパティDateTime常に equals を値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.AddMonths(time, months), time.Kind);
returnTime = DateTime.SpecifyKind(cal.AddMonths(time, months), time.Kind)

適用対象

こちらもご覧ください