PersianCalendar.AddYears(DateTime, Int32) 方法

定义

返回一个基于指定 DateTime 对象偏移指定年数的 DateTime 对象。

public:
 override DateTime AddYears(DateTime time, int years);
public override DateTime AddYears (DateTime time, int years);
override this.AddYears : DateTime * int -> DateTime
Public Overrides Function AddYears (time As DateTime, years As Integer) As DateTime

参数

time
DateTime

将向其添加年数的 DateTime

years
Int32

要添加的正年数或负年数。

返回

DateTime

通过将指定的年数添加到指定的 DateTime 对象所得到的 DateTime 对象。

例外

生成的 DateTime 超出了支持的范围。

years 小于 -10,000 或大于 10,000。

示例

下面的代码示例演示方法的用法 AddYears

using System;
using System.Globalization;

class Sample
{
    public static void Main()
    {
      PersianCalendar jc = new PersianCalendar();
      DateTime thisDate = DateTime.Now;

        //--------------------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------------------
      Console.WriteLine("\n........... Selected Properties .....................\n");
      Console.Write("Eras:");
      foreach (int era in jc.Eras)
      {
         Console.WriteLine(" era = {0}", era);
      }
        //--------------------------------------------------------------------------------
      Console.WriteLine("\nTwoDigitYearMax = {0}", jc.TwoDigitYearMax);
        //--------------------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------------------
      Console.WriteLine("\n............ Selected Methods .......................\n");

        //--------------------------------------------------------------------------------
      Console.WriteLine("GetDayOfYear: day = {0}", jc.GetDayOfYear(thisDate));
        //--------------------------------------------------------------------------------
      Console.WriteLine("GetDaysInMonth: days = {0}",
                        jc.GetDaysInMonth( thisDate.Year, thisDate.Month,
                        PersianCalendar.PersianEra));
        //--------------------------------------------------------------------------------
      Console.WriteLine("GetDaysInYear: days = {0}",
                        jc.GetDaysInYear(thisDate.Year, PersianCalendar.PersianEra));
        //--------------------------------------------------------------------------------
      Console.WriteLine("GetLeapMonth: leap month (if any) = {0}",
                        jc.GetLeapMonth(thisDate.Year, PersianCalendar.PersianEra));
        //-------------------------------------------------------------
      Console.WriteLine("GetMonthsInYear: months in a year = {0}",
                        jc.GetMonthsInYear(thisDate.Year, PersianCalendar.PersianEra));
        //--------------------------------------------------------------------------------
      Console.WriteLine("IsLeapDay: This is a leap day = {0}",
                        jc.IsLeapDay(thisDate.Year, thisDate.Month, thisDate.Day,
                        PersianCalendar.PersianEra));
        //--------------------------------------------------------------------------------
      Console.WriteLine("IsLeapMonth: This is a leap month = {0}",
                        jc.IsLeapMonth(thisDate.Year, thisDate.Month,
                        PersianCalendar.PersianEra));
        //--------------------------------------------------------------------------------
      Console.WriteLine("IsLeapYear: 1370 is a leap year = {0}",
                        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.

      Console.WriteLine("ToFourDigitYear:");
      Console.WriteLine("  If TwoDigitYearMax = {0}, ToFourDigitYear(99) = {1}",
                         jc.TwoDigitYearMax, jc.ToFourDigitYear(99));
      jc.TwoDigitYearMax = thisDate.Year;
      Console.WriteLine("  If TwoDigitYearMax = {0}, ToFourDigitYear(99) = {1}",
                        jc.TwoDigitYearMax, jc.ToFourDigitYear(99));
    }
}
// The example displays the following output:
//       ........... Selected Properties .....................
//
//       Eras: era = 1
//
//       TwoDigitYearMax = 99
//
//       ............ Selected Methods .......................
//
//       GetDayOfYear: day = 1
//       GetDaysInMonth: days = 31
//       GetDaysInYear: days = 365
//       GetLeapMonth: leap month (if any) = 0
//       GetMonthsInYear: months in a year = 12
//       IsLeapDay: This is a leap day = False
//       IsLeapMonth: This is a leap month = False
//       IsLeapYear: 1370 is a leap year = True
//       ToFourDigitYear:
//         If TwoDigitYearMax = 99, ToFourDigitYear(99) = 99
//         If TwoDigitYearMax = 2012, ToFourDigitYear(99) = 1999
Imports System.Globalization

Class Sample
    Public Shared Sub Main()
        '--------------------------------------------------------------------------------
        ' Get today's date.
        '--------------------------------------------------------------------------------
        Dim jc As New PersianCalendar()
        Dim thisDate As Date = Date.Now

        '--------------------------------------------------------------------------------
        ' Properties
        '--------------------------------------------------------------------------------
        Console.WriteLine(vbCrLf & _
                          "........... Selected Properties ....................." & vbCrLf)
        Console.Write("Eras:")
        Dim era As Integer
        For Each era In jc.Eras
            Console.WriteLine(" era = {0}", era)
        Next era
        '--------------------------------------------------------------------------------
        Console.WriteLine("TwoDigitYearMax = {0}", jc.TwoDigitYearMax)
        '--------------------------------------------------------------------------------
        ' Methods
        '--------------------------------------------------------------------------------
        Console.WriteLine(vbCrLf & _
                          "............ Selected Methods ......................." & vbCrLf)

        '--------------------------------------------------------------------------------
        Console.WriteLine("GetDayOfYear: day = {0}", jc.GetDayOfYear(thisDate))
        '--------------------------------------------------------------------------------

        Console.WriteLine("GetDaysInMonth: days = {0}", _
                           jc.GetDaysInMonth(thisDate.Year, _
                                             thisDate.Month, _
                                             PersianCalendar.PersianEra))
        '--------------------------------------------------------------------------------
        Console.WriteLine("GetDaysInYear: days = {0}", _
                          jc.GetDaysInYear(thisDate.Year, PersianCalendar.PersianEra))
        '--------------------------------------------------------------------------------
        Console.WriteLine("GetLeapMonth: leap month (if any) = {0}", _
                           jc.GetLeapMonth(thisDate.Year, PersianCalendar.PersianEra))
        '--------------------------------------------------------------------------------
        Console.WriteLine("GetMonthsInYear: months in a year = {0}", _
                           jc.GetMonthsInYear(thisDate.Year, PersianCalendar.PersianEra))
        '--------------------------------------------------------------------------------
        Console.WriteLine("IsLeapDay: This is a leap day = {0}", _
                           jc.IsLeapDay(thisDate.Year, _
                                        thisDate.Month, thisDate.Day, _
                                        PersianCalendar.PersianEra))
        '--------------------------------------------------------------------------------
        Console.WriteLine("IsLeapMonth: This is a leap month = {0}", _
                           jc.IsLeapMonth(thisDate.Year, _
                                          thisDate.Month, _
                                          PersianCalendar.PersianEra))
        '--------------------------------------------------------------------------------
        Console.WriteLine("IsLeapYear: 1370 is a leap year = {0}", _
                           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.

        Console.WriteLine("ToFourDigitYear:")
        Console.WriteLine("  If TwoDigitYearMax = {0}, ToFourDigitYear(99) = {1}", _
                          jc.TwoDigitYearMax, jc.ToFourDigitYear(99))
        jc.TwoDigitYearMax = thisDate.Year
        Console.WriteLine("  If TwoDigitYearMax = {0}, ToFourDigitYear(99) = {1}", _
                          jc.TwoDigitYearMax, jc.ToFourDigitYear(99))
    End Sub
End Class 
' The example displays output like the following: 
'       ........... Seleted Properties .....................
'       
'       Eras: era = 1
'       
'       TwoDigitYearMax = 99
'       
'       ............ Selected Methods .......................
'       
'       GetDayOfYear: day = 1
'       GetDaysInMonth: days = 31
'       GetDaysInYear: days = 365
'       GetLeapMonth: leap month (if any) = 0
'       GetMonthsInYear: months in a year = 12
'       IsLeapDay: This is a leap day = False
'       IsLeapMonth: This is a leap month = False
'       IsLeapYear: 1370 is a leap year = True
'       ToFourDigitYear:
'         If TwoDigitYearMax = 99, ToFourDigitYear(99) = 99
'         If TwoDigitYearMax = 2012, ToFourDigitYear(99) = 1999

注解

Kind返回的值的属性 DateTime 始终相等 DateTimeKind.Unspecified 。 可以 Kind time 通过调用方法来保留参数的属性 DateTime.SpecifyKind ,如下面的示例所示。

returnTime = DateTime.SpecifyKind(cal.AddYears(time, years), time.Kind);
returnTime = DateTime.SpecifyKind(cal.AddYears(time, years), time.Kind)

适用于

另请参阅