DateTime.AddMonths(Int32) メソッド
定義
public:
DateTime AddMonths(int months);
public DateTime AddMonths (int months);
member this.AddMonths : int -> DateTime
Public Function AddMonths (months As Integer) As DateTime
パラメーター
- months
- Int32
月数。A number of months. months
パラメーターは、正または負のどちらの場合もあります。The months
parameter can be negative or positive.
戻り値
このインスタンスで表された日付と時刻と months
の合計を値とするオブジェクト。An object whose value is the sum of the date and time represented by this instance and months
.
例外
結果として返された DateTime が MinValue より小さいか、MaxValue より大きいです。The resulting DateTime is less than MinValue or greater than MaxValue.
- または --or-
months
が -120,000 未満であるか、120,000 を超えています。months
is less than -120,000 or greater than 120,000.
例
次の例では、0 ~ 15 か月の期間を、2015年12月の最後の日に加算します。The following example adds between zero and fifteen months to the last day of December, 2015. この場合、AddMonths メソッドは、毎月の最終日の日付を返し、うるう年を正常に処理します。In this case, the AddMonths method returns the date of the last day of each month, and successfully handles leap years.
using System;
public class Example
{
public static void Main()
{
var dat = new DateTime(2015, 12, 31);
for (int ctr = 0; ctr <= 15; ctr++)
Console.WriteLine(dat.AddMonths(ctr).ToString("d"));
}
}
// The example displays the following output:
// 12/31/2015
// 1/31/2016
// 2/29/2016
// 3/31/2016
// 4/30/2016
// 5/31/2016
// 6/30/2016
// 7/31/2016
// 8/31/2016
// 9/30/2016
// 10/31/2016
// 11/30/2016
// 12/31/2016
// 1/31/2017
// 2/28/2017
// 3/31/2017
Module Example
Public Sub Main()
Dim dat As Date = #12/31/2015#
For ctr As Integer = 0 To 15
Console.WriteLine(dat.AddMonths(ctr).ToString("d"))
Next
End Sub
End Module
' The example displays the following output:
' 12/31/2015
' 1/31/2016
' 2/29/2016
' 3/31/2016
' 4/30/2016
' 5/31/2016
' 6/30/2016
' 7/31/2016
' 8/31/2016
' 9/30/2016
' 10/31/2016
' 11/30/2016
' 12/31/2016
' 1/31/2017
' 2/28/2017
' 3/31/2017
注釈
このメソッドは、この DateTime オブジェクトの値を変更しません。This method does not change the value of this DateTime object. 代わりに、この操作の結果を値として持つ新しい DateTime オブジェクトを返します。Instead, it returns a new DateTime object whose value is the result of this operation.
AddMonths メソッドは、うるう年と月の日数を考慮して、結果として得られる月と年を計算し、結果の DateTime オブジェクトの日の部分を調整します。The AddMonths method calculates the resulting month and year, taking into account leap years and the number of days in a month, then adjusts the day part of the resulting DateTime object. 結果として得られる月の日付が有効な日付でない場合は、その月の最後の有効日が使用されます。If the resulting day is not a valid day in the resulting month, the last valid day of the resulting month is used. たとえば、3月31日 + 1 か月 = 4 月30日、3月31日-1 か月 = 28 年以外の場合は2月28日、閏年の場合は2月29日となります。For example, March 31st + 1 month = April 30th, and March 31st - 1 month = February 28 for a non-leap year and February 29 for a leap year.
結果の DateTime オブジェクトの時刻部分は、このインスタンスと同じままです。The time-of-day part of the resulting DateTime object remains the same as this instance.