Compartir a través de


PersianCalendar.AddMonths Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Returns a DateTime object that is offset the specified number of months from the specified DateTime object.

Namespace:  System.Globalization
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Overrides Function AddMonths ( _
    time As DateTime, _
    months As Integer _
) As DateTime
public override DateTime AddMonths(
    DateTime time,
    int months
)

Parameters

  • months
    Type: System.Int32
    The positive or negative number of months to add.

Return Value

Type: System.DateTime
The date and time that results from adding the number of months specified by the months parameter to the date specified by the time parameter.

Exceptions

Exception Condition
ArgumentException

The resulting DateTime is outside the supported range.

ArgumentOutOfRangeException

months is less than -120,000 or greater than 120,000.

Remarks

The Kind property of the returned DateTime value always equals DateTimeKind.Unspecified. You can preserve the Kind property of the time parameter by calling the DateTime.SpecifyKind method, as the following example shows.

returnTime = DateTime.SpecifyKind(cal.AddMonths(time, months), time.Kind)
returnTime = DateTime.SpecifyKind(cal.AddMonths(time, months), time.Kind);

Examples

The following example uses the AddMonths method.

using System;
using System.Globalization;

class Example
{
    public static void Demo(System.Windows.Controls.TextBlock outputBlock) 
    {
        //--------------------------------------------------------------------------------
        // Get today's date.
        //--------------------------------------------------------------------------------
      outputBlock.Text += "\n................. Today ...........................\n";
      PersianCalendar jc = new PersianCalendar();
      DateTime thisDate = DateTime.Now;

        // Display the current date using the Gregorian and Persian calendars.
      outputBlock.Text += "Today is:\n";
      outputBlock.Text += String.Format("   {0:dddd}, {0} in the Gregorian calendar.\n", thisDate);
      outputBlock.Text += String.Format("   {0}, {1}/{2}/{3} {4}:{5}:{6} in the Persian calendar.\n", 
                                        jc.GetDayOfWeek(thisDate), 
                                        jc.GetMonth(thisDate), 
                                        jc.GetDayOfMonth(thisDate), 
                                        jc.GetYear(thisDate), 
                                        jc.GetHour(thisDate), 
                                        jc.GetMinute(thisDate), 
                                        jc.GetSecond(thisDate));
        //--------------------------------------------------------------------------------
        // Fields
        //--------------------------------------------------------------------------------
      outputBlock.Text += "\n............... Fields .............................\n";
      outputBlock.Text += String.Format("PersianEra = {0}\n", PersianCalendar.PersianEra);
        //--------------------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------------------
      outputBlock.Text += "\n............... Properties .........................\n";
      outputBlock.Text += "Eras:";
      foreach (int era in jc.Eras)
      {
         outputBlock.Text += String.Format(" era = {0}\n", era);
      }
        //--------------------------------------------------------------------------------
      outputBlock.Text += "\nGregorian Date Range Supported by the Persian Calendar:\n";
      outputBlock.Text += String.Format("   From {0:G}\n", jc.MinSupportedDateTime);
      outputBlock.Text += String.Format("   To {0:G}\n", jc.MaxSupportedDateTime);
        //--------------------------------------------------------------------------------
      outputBlock.Text += String.Format("\nTwoDigitYearMax = {0}\n", jc.TwoDigitYearMax);
        //--------------------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------------------
      outputBlock.Text += "\n............ Selected Methods .......................\n";

        //--------------------------------------------------------------------------------
      outputBlock.Text += String.Format("GetDayOfYear: day = {0}\n", jc.GetDayOfYear(thisDate));
        //--------------------------------------------------------------------------------
      outputBlock.Text += String.Format("GetDaysInMonth: days = {0}\n", 
                                        jc.GetDaysInMonth( thisDate.Year, thisDate.Month, 
                                        PersianCalendar.PersianEra));
        //--------------------------------------------------------------------------------
      outputBlock.Text += String.Format("GetDaysInYear: days = {0}\n", 
                        jc.GetDaysInYear(thisDate.Year, PersianCalendar.PersianEra));
        //--------------------------------------------------------------------------------
      outputBlock.Text += String.Format("GetLeapMonth: leap month (if any) = {0}\n", 
                        jc.GetLeapMonth(thisDate.Year, PersianCalendar.PersianEra));
        //-------------------------------------------------------------
      outputBlock.Text += String.Format("GetMonthsInYear: months in a year = {0}\n", 
                        jc.GetMonthsInYear(thisDate.Year, PersianCalendar.PersianEra));
        //--------------------------------------------------------------------------------
      outputBlock.Text += String.Format("IsLeapDay: This is a leap day = {0}\n", 
                        jc.IsLeapDay(thisDate.Year, thisDate.Month, thisDate.Day, 
                        PersianCalendar.PersianEra));
        //--------------------------------------------------------------------------------
      outputBlock.Text += String.Format("IsLeapMonth: This is a leap month = {0}\n", 
                        jc.IsLeapMonth(thisDate.Year, thisDate.Month, 
                        PersianCalendar.PersianEra));
        //--------------------------------------------------------------------------------
      outputBlock.Text += String.Format("IsLeapYear: 1370 is a leap year = {0}\n", 
                        jc.IsLeapYear(1370, PersianCalendar.PersianEra));
        //--------------------------------------------------------------------------------

        // Get the 4-digit year for a year whose last two digits are 99. The 4-digit year 
        // depends on the current value of the TwoDigitYearMax property.

      outputBlock.Text += "ToFourDigitYear:\n";
      outputBlock.Text += String.Format("  If TwoDigitYearMax = {0}, ToFourDigitYear(99) = {1}\n", 
                         jc.TwoDigitYearMax, jc.ToFourDigitYear(99));
      jc.TwoDigitYearMax = thisDate.Year;
      outputBlock.Text += String.Format("  If TwoDigitYearMax = {0}, ToFourDigitYear(99) = {1}\n", 
                        jc.TwoDigitYearMax, jc.ToFourDigitYear(99));
    }
}
/*
The example produces the following results:

................. Today ...........................

Today is Wednesday, 6/14/2006 11:31:13 AM

............... Fields .............................

PersianEra = 1

............... Properties .........................

Eras: era = 1
MaxSupportedDateTime = 12/31/9999 11:59:59 PM
MinSupportedDateTime = 3/21/0622 12:00:00 AM
TwoDigitYearMax = 1410

................ Methods ...........................

AddMonths: thisDate + 6 months = 12/15/2006 11:31:13 AM
           thisDate - 6 months = 12/15/2005 11:31:13 AM
AddYears:  thisDate + 5 years =  6/14/2011 11:31:13 AM
           thisDate - 3 years =  6/14/2003 11:31:13 AM
GetDayOfMonth: month = 24
GetDayOfWeek: day = Wednesday
GetDayOfYear: day = 86
GetDaysInMonth: days = 31
GetDaysInYear: days = 366
GetEra: era = 1
GetLeapMonth: leap month (if any) = 0
GetMonth: month = 3
GetMonthsInYear: months in a year = 12
GetYear: year = 1385
IsLeapDay: This is a leap day = False
IsLeapMonth: This is a leap month = False
IsLeapYear: 1370 is a leap year = True
ToDateTime:
  Gregorian calendar: 6/10/2006 8:30:15 PM
  Persian calendar:   3/20/1385 8:30:15 PM
ToFourDigitYear:
  If TwoDigitYearMax = 1410, ToFourDigitYear(99) = 1399
  If TwoDigitYearMax = 2006, ToFourDigitYear(99) = 1999

*/

Version Information

Silverlight

Supported in: 5

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.