共用方式為


UmAlQuraCalendar.AddMonths(DateTime, Int32) 方法

定義

計算與指定初始日期相差指定月數的日期。

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

參數

time
DateTime

要在其中加入月份的日期。 UmAlQuraCalendar 類別只支援從 04/30/1900 00.00.00 (西曆日期) 到 11/16/2077 23:59:59 (西曆日期) 的日期。

months
Int32

要加入的正或負月數。

傳回

months 參數指定的月數加入至由 time 參數指定的日期所產生的日期。

例外狀況

產生的日期不在 UmAlQuraCalendar 類別支援的範圍內。

months 小於 -120,000 或大於 120,000。

-或-

time 超出此曆法所支援的範圍。

範例

下列範例會具現化 DateTime 值,並在 Um Al Qura 行事曆中顯示其數位元件的值。 接下來,它會呼叫 AddYearsAddMonths 方法,將 Um Al Qura 行事曆中的 2 年和 10 個月新增至日期值。 最後,它會再次在Um Al Qura行事曆中顯示這些日期元件的值。

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      DateTime date1 = new DateTime(2011, 4, 3, new GregorianCalendar());
      Calendar cal = new UmAlQuraCalendar();

      Console.WriteLine("{0:MMMM d, yyyy} in the Gregorian calendar is equivalent to:", date1);
      DisplayCalendarInfo(cal, date1);

      // Add 2 years and 10 months by calling UmAlQuraCalendar methods.
      date1 = cal.AddYears(date1, 2);
      date1 = cal.AddMonths(date1, 10);

      Console.WriteLine("After adding 2 years and 10 months in the {0} calendar,",
                        GetCalendarName(cal));
      Console.WriteLine("{0:MMMM d, yyyy} in the Gregorian calendar is equivalent to:", date1);
      DisplayCalendarInfo(cal, date1);
   }

   private static void DisplayCalendarInfo(Calendar cal, DateTime date1)
   {
      Console.WriteLine("   Calendar:   {0}", GetCalendarName(cal));
      Console.WriteLine("   Era:        {0}", cal.GetEra(date1));
      Console.WriteLine("   Year:       {0}", cal.GetYear(date1));
      Console.WriteLine("   Month:      {0}", cal.GetMonth(date1));
      Console.WriteLine("   DayOfYear:  {0}", cal.GetDayOfYear(date1));
      Console.WriteLine("   DayOfMonth: {0}", cal.GetDayOfMonth(date1));
      Console.WriteLine("   DayOfWeek:  {0}\n", cal.GetDayOfWeek(date1));
   }

   private static string GetCalendarName(Calendar cal)
   {
      return cal.ToString().Replace("System.Globalization.", "").
             Replace("Calendar", "");
   }
}
// The example displays the following output:
//    April 3, 2011 in the Gregorian calendar is equivalent to:
//       Calendar:   UmAlQura
//       Era:        1
//       Year:       1432
//       Month:      4
//       DayOfYear:  118
//       DayOfMonth: 29
//       DayOfWeek:  Sunday
//
//    After adding 2 years and 10 months in the UmAlQura calendar,
//    January 1, 2014 in the Gregorian calendar is equivalent to:
//       Calendar:   UmAlQura
//       Era:        1
//       Year:       1435
//       Month:      2
//       DayOfYear:  59
//       DayOfMonth: 29
//       DayOfWeek:  Wednesday
Imports System.Globalization

Module Example
   Public Sub Main()
      Dim date1 As Date = New Date(2011, 4, 3, New GregorianCalendar())
      Dim cal As New UmAlQuraCalendar()
      
      Console.WriteLine("{0:MMMM d, yyyy} in the Gregorian calendar is equivalent to:", date1)
      DisplayCalendarInfo(cal, date1)
            
      ' Add 2 years and 10 months by calling UmAlQuraCalendar methods.
      date1 = cal.AddYears(date1, 2)
      date1 = cal.AddMonths(date1, 10)       

      Console.WriteLine("After adding 2 years and 10 months in the {0} calendar,", 
                        GetCalendarName(cal))
      Console.WriteLine("{0:MMMM d, yyyy} in the Gregorian calendar is equivalent to:", date1)
      DisplayCalendarInfo(cal, date1)
   End Sub
   
   Private Sub DisplayCalendarInfo(cal As Calendar, date1 As Date)
      Console.WriteLine("   Calendar:   {0}", GetCalendarName(cal))    
      Console.WriteLine("   Era:        {0}", cal.GetEra(date1))
      Console.WriteLine("   Year:       {0}", cal.GetYear(date1))
      Console.WriteLine("   Month:      {0}", cal.GetMonth(date1))
      Console.WriteLine("   DayOfYear:  {0}", cal.GetDayOfYear(date1))
      Console.WriteLine("   DayOfMonth: {0}", cal.GetDayOfMonth(date1))
      Console.WriteLine("   DayOfWeek:  {0}", cal.GetDayOfWeek(date1))
      Console.WriteLine()
   End Sub
   
   Private Function GetCalendarName(cal As Calendar) As String
      Return cal.ToString().Replace("System.Globalization.", "").
             Replace("Calendar", "")   
   End Function
End Module
' The example displays the following output:
'    April 3, 2011 in the Gregorian calendar is equivalent to:
'       Calendar:   UmAlQura
'       Era:        1
'       Year:       1432
'       Month:      4
'       DayOfYear:  118
'       DayOfMonth: 29
'       DayOfWeek:  Sunday
'    
'    After adding 2 years and 10 months in the UmAlQura calendar,
'    January 1, 2014 in the Gregorian calendar is equivalent to:
'       Calendar:   UmAlQura
'       Era:        1
'       Year:       1435
'       Month:      2
'       DayOfYear:  59
'       DayOfMonth: 29
'       DayOfWeek:  Wednesday

備註

如果產生的日期不是所產生年份之月份的有效日期,則產生的日期部分 DateTime 會受到影響。 它變更為結果年份結果月份的最後一個有效日。 如果產生的 DateTime 月份超出指定的 DateTime年份,則產生的年份部分會受到影響。 此實作僅支援目前的紀元。 因此,如果產生的年份超出指定 DateTime之 的紀元,就會擲回例外狀況。 結果 DateTime 的當日時間部分會維持與指定的 DateTime相同。

例如,如果指定的月份是 Zulkadah,其有 30 天,則指定的日期是該月份的第 30 天,而 參數的值 months 是 3,產生的年份是一個以上的指定年份,產生的月份是 Safar,而產生的日期是第 29 天,也就是 Safar 的最後一天。

如果參數的值是負數 months ,則產生的 DateTime 值早於指定的 DateTime

KindDateTime回值的 屬性一律等於 DateTimeKind.Unspecified。 您可以呼叫 DateTime.SpecifyKind 方法來保留 Kind 參數的 time 屬性,如下列範例所示。

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

適用於