JapaneseCalendar.AddMonths(DateTime, Int32) Methode
Definition
Wichtig
Einige Informationen beziehen sich auf Vorabversionen, die vor dem Release ggf. grundlegend überarbeitet werden. Microsoft übernimmt hinsichtlich der hier bereitgestellten Informationen keine Gewährleistungen, seien sie ausdrücklich oder konkludent.
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
Parameter
- months
- Int32
Die Anzahl der hinzuzufügenden Monate.
Gibt zurück
Das DateTime ergebnis aus dem Hinzufügen der angegebenen Anzahl von Monaten zum angegebenen DateTime.
Ausnahmen
Das Ergebnis DateTime liegt außerhalb des unterstützten Bereichs.
Beispiele
Im folgenden Codebeispiel werden die Werte mehrerer Komponenten eines DateTime in Bezug auf den japanischen Kalender angezeigt.
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
Hinweise
Der Tagesteil des resultierenden DateTime Ergebnisses ist betroffen, wenn der resultierende Tag kein gültiger Tag im resultierenden Monat des resultierenden Jahres ist. Sie wird in dem resultierenden Monat des resultierenden Jahres in den letzten gültigen Tag geändert. Der Jahresteil des resultierenden DateTime Ergebnisses ist betroffen, wenn sich der resultierende Monat außerhalb des angegebenen DateTimeJahres befindet. Der Era-Teil des resultierenden DateTime Zeitraums wird beeinflusst, wenn das resultierende Jahr außerhalb der Zeit des angegebenen DateTimeliegt. Der Zeit-of-Day-Teil des Resultierenden DateTime bleibt identisch mit dem angegebenen DateTime.
Wenn der angegebene Monat beispielsweise Juugatsu (Oktober) ist, der 31 Tage hat, ist der angegebene Tag der 31. Tag dieses Monats und months ist 6, das resultierende Jahr ist ein mehr als das angegebene Jahr, der resultierende Monat ist Shigatsu (April), und der resultierende Tag ist der 30. Tag, der letzte Tag in Shigatsu (April).
Wenn months dies negativ ist, liegt das Ergebnis DateTime vor dem angegebenen DateTime.
Die Kind Eigenschaft des zurückgegebenen DateTime Werts DateTimeKind.Unspecifiedist immer gleich. Sie können die Kind Eigenschaft des time Parameters beibehalten, indem Sie die DateTime.SpecifyKind Methode aufrufen, wie im folgenden Beispiel gezeigt.
returnTime = DateTime.SpecifyKind(cal.AddMonths(time, months), time.Kind);
returnTime = DateTime.SpecifyKind(cal.AddMonths(time, months), time.Kind)