英語で読む

次の方法で共有


KoreanCalendar.AddMonths(DateTime, Int32) メソッド

定義

指定した DateTime から指定した月数が経過した後の DateTime を返します。

C#
public override DateTime AddMonths (DateTime time, int months);

パラメーター

time
DateTime

月数を加算する対象の DateTime

months
Int32

加算する月の数。

戻り値

指定した DateTime に指定した月数を加算した結果の DateTime

例外

months が -120000 未満です。

- または -

months が 120000 を超えています。

次のコード例では、 の複数のコンポーネントの値を DateTime 韓国語カレンダーの観点から表示します。

C#
using System;
using System.Globalization;

public class SamplesKoreanCalendar  {

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

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

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

*/

注釈

結果の日が、結果の年の結果 DateTime の月の有効な日でない場合、結果の日の部分が影響を受ける。 結果の年の結果の月の最後の有効な日に変更されます。 結果 DateTime の月が指定した DateTimeの年外の場合、結果の の年部分が影響を受ける。 この実装では、現在の時代 (年号) のみがサポートされます。 したがって、 は、 ArgumentException 結果の年が指定された の時代 (年号) の範囲外の場合にスローされます DateTime。 結果 DateTime のの時刻部分は、指定した DateTimeと同じままです。

たとえば、指定した月が 31 日の 10 月で、指定した日がその月の 31 日で、パラメーターの値が 6 の場合、結果の months 年は指定した年より 1 つ多く、結果の月は 4 月、結果の日は 4 月の最終日である 30 日になります。

パラメーターの値が負の months 場合、結果 DateTime は指定された DateTimeよりも前になります。

戻りDateTime値の プロパティはKind常に とDateTimeKind.Unspecified等しくなります。 次の例に Kind 示すように、 メソッドを time 呼び出 DateTime.SpecifyKind すことで、 パラメーターの プロパティを保持できます。

C#
returnTime = DateTime.SpecifyKind(cal.AddMonths(time, months), time.Kind);

適用対象

製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

こちらもご覧ください