Поделиться через


PersianCalendar.AddYears Method

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

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

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

Syntax

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

Parameters

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

Return Value

Type: System.DateTime
The date and time that results from adding the specified number of years to the specified DateTime object.

Exceptions

Exception Condition
ArgumentException

The resulting DateTime is outside the supported range.

ArgumentOutOfRangeException

years is less than -10,000 or greater than 10,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.AddYears(time, years), time.Kind)
returnTime = DateTime.SpecifyKind(cal.AddYears(time, years), time.Kind);

Examples

The following example uses the AddYears 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.