HebrewCalendar.AddMonths(DateTime, Int32) Método
Definição
Importante
Algumas informações dizem respeito a um produto pré-lançado que pode ser substancialmente modificado antes de ser lançado. A Microsoft não faz garantias, de forma expressa ou implícita, em relação à informação aqui apresentada.
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
Parâmetros
- months
- Int32
O número de meses a acrescentar.
Devoluções
O DateTime resultado resulta da soma do número especificado de meses ao especificado DateTime.
Exceções
O resultado DateTime está fora do alcance suportado.
months é inferior a -120.000 ou superior a 120.000.
Exemplos
O seguinte exemplo de código mostra os valores de vários componentes de a DateTime em termos do calendário hebraico.
using System;
using System.Globalization;
public class SamplesHebrewCalendar {
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 HebrewCalendar.
HebrewCalendar myCal = new HebrewCalendar();
// Displays the values of the DateTime.
Console.WriteLine( "April 3, 2002 of the Gregorian calendar equals the following in the Hebrew 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 Hebrew calendar:
Era: 1
Year: 5762
Month: 7
DayOfYear: 198
DayOfMonth: 21
DayOfWeek: Wednesday
After adding two years and ten months:
Era: 1
Year: 5765
Month: 5
DayOfYear: 138
DayOfMonth: 21
DayOfWeek: Monday
*/
Imports System.Globalization
Public Class SamplesHebrewCalendar
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 HebrewCalendar.
Dim myCal As New HebrewCalendar()
' Displays the values of the DateTime.
Console.WriteLine("April 3, 2002 of the Gregorian calendar equals the following in the Hebrew 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 Hebrew calendar:
' Era: 1
' Year: 5762
' Month: 7
' DayOfYear: 198
' DayOfMonth: 21
' DayOfWeek: Wednesday
'
'After adding two years and ten months:
' Era: 1
' Year: 5765
' Month: 5
' DayOfYear: 138
' DayOfMonth: 21
' DayOfWeek: Monday
Observações
Esta implementação da HebrewCalendar classe reconhece apenas os anos hebraicos de 5343 a 5999 (1583 a 2239 no calendário gregoriano).
A parte do dia resultante DateTime é afetada se o dia resultante não for válido no mês resultante do ano resultante. É alterado para o último dia válido do mês resultante do ano seguinte. A parte do ano do resultado DateTime é afetada se o mês resultante estiver fora do ano especificado DateTime. Esta implementação suporta apenas a era atual. Portanto, ArgumentException é lançado se o ano resultante estiver fora da era do especificado DateTime. A parte do dia do resultado DateTime mantém-se igual à especificada DateTime.
Por exemplo, se o mês especificado for Av, que tem 30 dias, o dia especificado for o 30.º dia desse mês, e o valor do months parâmetro for 5, o ano resultante for um a mais do que o ano especificado, o mês resultante for Tevet, e o dia resultante for o 29.º dia, que é o último dia em Tevet.
Se o valor do months parâmetro for negativo, o resultado DateTime é anterior ao especificado DateTime.
A Kind propriedade do valor devolvido DateTime é DateTimeKind.Unspecifiedsempre igual a . Pode preservar a Kind propriedade do time parâmetro chamando o DateTime.SpecifyKind método, como mostra o exemplo seguinte.
returnTime = DateTime.SpecifyKind(cal.AddMonths(time, months), time.Kind);
returnTime = DateTime.SpecifyKind(cal.AddMonths(time, months), time.Kind)