KoreanCalendar.AddYears(DateTime, Int32) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
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
參數
- years
- Int32
要新增的年數。
傳回
DateTime,由將指定的年數加入指定的 DateTime 所產生。
例外狀況
years
或 time
超出範圍。
範例
下列程式代碼範例會根據韓曆顯示的數個 DateTime 元件值。
using namespace System;
using namespace System::Globalization;
void DisplayValues( Calendar^ myCal, DateTime myDT )
{
Console::WriteLine( " Era: {0}", myCal->GetEra( myDT ) );
Console::WriteLine( " Year: {0}", myCal->GetYear( myDT ) );
Console::WriteLine( " Month: {0}", myCal->GetMonth( myDT ) );
Console::WriteLine( " DayOfYear: {0}", myCal->GetDayOfYear( myDT ) );
Console::WriteLine( " DayOfMonth: {0}", myCal->GetDayOfMonth( myDT ) );
Console::WriteLine( " DayOfWeek: {0}", myCal->GetDayOfWeek( myDT ) );
Console::WriteLine();
}
int main()
{
// Sets a DateTime to April 3, 2002 of the Gregorian calendar.
DateTime myDT = DateTime(2002,4,3,gcnew GregorianCalendar);
// Creates an instance of the KoreanCalendar.
KoreanCalendar^ myCal = gcnew KoreanCalendar;
// Displays the values of the DateTime.
Console::WriteLine( "April 3, 2002 of the Gregorian calendar equals the following in the Korean calendar:" );
DisplayValues( myCal, myDT );
// Adds two years and ten months.
myDT = myCal->AddYears( myDT, 2 );
myDT = myCal->AddMonths( myDT, 10 );
// Displays the values of the DateTime.
Console::WriteLine( "After adding two years and ten months:" );
DisplayValues( myCal, myDT );
}
/*
This code produces the following output.
April 3, 2002 of the Gregorian calendar equals the following in the Korean calendar:
Era: 1
Year: 4335
Month: 4
DayOfYear: 93
DayOfMonth: 3
DayOfWeek: Wednesday
After adding two years and ten months:
Era: 1
Year: 4338
Month: 2
DayOfYear: 34
DayOfMonth: 3
DayOfWeek: Thursday
*/
using System;
using System.Globalization;
public class SamplesKoreanCalendar {
public static void Main() {
// Sets a DateTime to April 3, 2002 of the Gregorian calendar.
DateTime myDT = new DateTime( 2002, 4, 3, new GregorianCalendar() );
// Creates an instance of the KoreanCalendar.
KoreanCalendar myCal = new KoreanCalendar();
// Displays the values of the DateTime.
Console.WriteLine( "April 3, 2002 of the Gregorian calendar equals the following in the Korean calendar:" );
DisplayValues( myCal, myDT );
// Adds two years and ten months.
myDT = myCal.AddYears( myDT, 2 );
myDT = myCal.AddMonths( myDT, 10 );
// Displays the values of the DateTime.
Console.WriteLine( "After adding two years and ten months:" );
DisplayValues( myCal, myDT );
}
public static void DisplayValues( Calendar myCal, DateTime myDT ) {
Console.WriteLine( " Era: {0}", myCal.GetEra( myDT ) );
Console.WriteLine( " Year: {0}", myCal.GetYear( myDT ) );
Console.WriteLine( " Month: {0}", myCal.GetMonth( myDT ) );
Console.WriteLine( " DayOfYear: {0}", myCal.GetDayOfYear( myDT ) );
Console.WriteLine( " DayOfMonth: {0}", myCal.GetDayOfMonth( myDT ) );
Console.WriteLine( " DayOfWeek: {0}", myCal.GetDayOfWeek( myDT ) );
Console.WriteLine();
}
}
/*
This code produces the following output.
April 3, 2002 of the Gregorian calendar equals the following in the Korean calendar:
Era: 1
Year: 4335
Month: 4
DayOfYear: 93
DayOfMonth: 3
DayOfWeek: Wednesday
After adding two years and ten months:
Era: 1
Year: 4338
Month: 2
DayOfYear: 34
DayOfMonth: 3
DayOfWeek: Thursday
*/
Imports System.Globalization
Public Class SamplesKoreanCalendar
Public Shared Sub Main()
' Sets a DateTime to April 3, 2002 of the Gregorian calendar.
Dim myDT As New DateTime(2002, 4, 3, New GregorianCalendar())
' Creates an instance of the KoreanCalendar.
Dim myCal As New KoreanCalendar()
' Displays the values of the DateTime.
Console.WriteLine("April 3, 2002 of the Gregorian calendar equals the following in the Korean calendar:")
DisplayValues(myCal, myDT)
' Adds two years and ten months.
myDT = myCal.AddYears(myDT, 2)
myDT = myCal.AddMonths(myDT, 10)
' Displays the values of the DateTime.
Console.WriteLine("After adding two years and ten months:")
DisplayValues(myCal, myDT)
End Sub
Public Shared Sub DisplayValues(myCal As Calendar, myDT As DateTime)
Console.WriteLine(" Era: {0}", myCal.GetEra(myDT))
Console.WriteLine(" Year: {0}", myCal.GetYear(myDT))
Console.WriteLine(" Month: {0}", myCal.GetMonth(myDT))
Console.WriteLine(" DayOfYear: {0}", myCal.GetDayOfYear(myDT))
Console.WriteLine(" DayOfMonth: {0}", myCal.GetDayOfMonth(myDT))
Console.WriteLine(" DayOfWeek: {0}", myCal.GetDayOfWeek(myDT))
Console.WriteLine()
End Sub
End Class
'This code produces the following output.
'
'April 3, 2002 of the Gregorian calendar equals the following in the Korean calendar:
' Era: 1
' Year: 4335
' Month: 4
' DayOfYear: 93
' DayOfMonth: 3
' DayOfWeek: Wednesday
'
'After adding two years and ten months:
' Era: 1
' Year: 4338
' Month: 2
' DayOfYear: 34
' DayOfMonth: 3
' DayOfWeek: Thursday
備註
如果產生的日期不是所產生年份之月份的有效日期,則產生的日期部分 DateTime 會受到影響。 它變更為結果年份結果月份的最後一個有效日。 產生的 DateTime 月份部分會維持與指定的 DateTime相同。 此實作僅支援目前的紀元。 因此, ArgumentException 如果產生的年份超出指定 DateTime之 的紀元,則會擲回 。 結果 DateTime 的當日時間部分會維持與指定的 DateTime相同。
例如,2 月有 28 天,但當閏年有 29 天時除外。 如果指定的日期是閏年 2 月的第 29 天,而的值 years
是 1,則產生的日期是下一年 2 月的第 28 天。
如果 years
為負數,則產生的 DateTime 值早於指定的 DateTime。
傳KindDateTime回值的 屬性一律等於 DateTimeKind.Unspecified。 您可以呼叫 DateTime.SpecifyKind 方法來保留 Kind 參數的 time
屬性,如下列範例所示。
returnTime = DateTime.SpecifyKind(cal.AddYears(time, years), time.Kind);
returnTime = DateTime.SpecifyKind(cal.AddYears(time, years), time.Kind)