System.Globalization.GregorianCalendar.AddMonths C# (CSharp) Méthode

AddMonths() public méthode

public AddMonths ( System.DateTime time, int months ) : System.DateTime
time System.DateTime
months int
Résultat System.DateTime
        public override DateTime AddMonths(DateTime time, int months)
        {
            if (months < -120000 || months > 120000) {
                throw new ArgumentOutOfRangeException(
                            "months",
                            String.Format(
                                CultureInfo.CurrentCulture,
                                Environment.GetResourceString("ArgumentOutOfRange_Range"),
                                -120000,
                                120000));
            }
            int y = GetDatePart(time.Ticks, DatePartYear);
            int m = GetDatePart(time.Ticks, DatePartMonth);
            int d = GetDatePart(time.Ticks, DatePartDay);
            int i = m - 1 + months;
            if (i >= 0)
            {
                m = i % 12 + 1;
                y = y + i / 12;
            }
            else
            {
                m = 12 + (i + 1) % 12;
                y = y + (i - 11) / 12;
            }
            int[] daysArray = (y % 4 == 0 && (y % 100 != 0 || y % 400 == 0)) ? DaysToMonth366: DaysToMonth365;
            int days = (daysArray[m] - daysArray[m - 1]);

            if (d > days)
            {
                d = days;
            }
            long ticks = DateToTicks(y, m, d) + time.Ticks % TicksPerDay;
            Calendar.CheckAddResult(ticks, MinSupportedDateTime, MaxSupportedDateTime);

            return (new DateTime(ticks));
        }

Usage Example

 public void PosTest3()
 {
     DateTime initialTime;
     int months;
     DateTime resultingTime;
     System.Globalization.Calendar myCalendar = new GregorianCalendar(GregorianCalendarTypes.USEnglish);
     months = -1 * _generator.GetInt32(-55) % c_MAX_MONTHS_NUMBER - 1;
     initialTime = myCalendar.MaxSupportedDateTime;
     resultingTime = myCalendar.AddMonths(initialTime, months);
     VerifyAddMonthsResult(myCalendar, initialTime, resultingTime, months);
 }
All Usage Examples Of System.Globalization.GregorianCalendar::AddMonths