JapaneseCalendar.AddMonths(DateTime, Int32) メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
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
パラメーター
- months
- Int32
追加する月数。
返品
指定した月数を指定したDateTimeに追加した結果のDateTime。
例外
結果として得られる DateTime は、サポートされている範囲外です。
例
次のコード例では、日本語カレンダーの観点から、 DateTime のいくつかのコンポーネントの値を表示します。
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.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
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 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 の日部分は、結果の日が結果の年の結果の月の有効な日でない場合に影響を受けます。 結果の年の結果の月の最後の有効な日に変更されます。 結果の月が指定したDateTimeの年外の場合、結果のDateTimeの年部分が影響を受けます。 結果の DateTime の元号部分は、結果の年が指定した DateTimeの時代 (年号) の範囲外である場合に影響を受ける。 結果の DateTime の時刻部分は、指定した DateTimeと同じままです。
たとえば、指定した月が 31 日の十月 (10 月) で、指定した日がその月の 31 日、 months が 6 で、結果の年が指定した年より 1 つ多い場合、結果の月は志賀津 (4 月)、結果の日は 30 日 (4 月) です。
monthsが負の値の場合、結果のDateTimeは指定されたDateTimeよりも前になります。
返されるKind値のDateTimeプロパティは常にDateTimeKind.Unspecifiedと等しくなります。 次の例に示すように、Kind メソッドを呼び出すことで、time パラメーターのDateTime.SpecifyKind プロパティを保持できます。
returnTime = DateTime.SpecifyKind(cal.AddMonths(time, months), time.Kind);
returnTime = DateTime.SpecifyKind(cal.AddMonths(time, months), time.Kind)