當前位置: 首頁>>代碼示例>>C#>>正文


C# DateTime.AddYears方法代碼示例

本文整理匯總了C#中System.DateTime.AddYears方法的典型用法代碼示例。如果您正苦於以下問題:C# DateTime.AddYears方法的具體用法?C# DateTime.AddYears怎麽用?C# DateTime.AddYears使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.DateTime的用法示例。


在下文中一共展示了DateTime.AddYears方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: Main

//引入命名空間
using System;

public class Example
{
   public static void Main()
   {
      DateTime baseDate = new DateTime(2000, 2, 29);
      Console.WriteLine("    Base Date:        {0:d}\n", baseDate);
      
      // Show dates of previous fifteen years.
      for (int ctr = -1; ctr >= -15; ctr--)
         Console.WriteLine("{0,2} year(s) ago:        {1:d}", 
                           Math.Abs(ctr), baseDate.AddYears(ctr));
      Console.WriteLine();

      // Show dates of next fifteen years.
      for (int ctr = 1; ctr <= 15; ctr++)
         Console.WriteLine("{0,2} year(s) from now:   {1:d}", 
                           ctr, baseDate.AddYears(ctr));
   }
}
開發者ID:.NET開發者,項目名稱:System,代碼行數:22,代碼來源:DateTime.AddYears

輸出:

Base Date:        2/29/2000

1 year(s) ago:        2/28/1999
2 year(s) ago:        2/28/1998
3 year(s) ago:        2/28/1997
4 year(s) ago:        2/29/1996
5 year(s) ago:        2/28/1995
6 year(s) ago:        2/28/1994
7 year(s) ago:        2/28/1993
8 year(s) ago:        2/29/1992
9 year(s) ago:        2/28/1991
10 year(s) ago:        2/28/1990
11 year(s) ago:        2/28/1989
12 year(s) ago:        2/29/1988
13 year(s) ago:        2/28/1987
14 year(s) ago:        2/28/1986
15 year(s) ago:        2/28/1985

1 year(s) from now:   2/28/2001
2 year(s) from now:   2/28/2002
3 year(s) from now:   2/28/2003
4 year(s) from now:   2/29/2004
5 year(s) from now:   2/28/2005
6 year(s) from now:   2/28/2006
7 year(s) from now:   2/28/2007
8 year(s) from now:   2/29/2008
9 year(s) from now:   2/28/2009
10 year(s) from now:   2/28/2010
11 year(s) from now:   2/28/2011
12 year(s) from now:   2/29/2012
13 year(s) from now:   2/28/2013
14 year(s) from now:   2/28/2014
15 year(s) from now:   2/28/2015

示例2: Main

//引入命名空間
using System;

class MainClass
{
  public static void Main()
  {
    DateTime myDateTime9 = new DateTime(2004, 1, 1);
    Console.WriteLine("Initial myDateTime9 = " + myDateTime9);
    myDateTime9 = myDateTime9.AddYears(1);
    myDateTime9 = myDateTime9.AddMonths(5);
    myDateTime9 = myDateTime9.AddDays(3);
    myDateTime9 = myDateTime9.AddMinutes(30);
    myDateTime9 = myDateTime9.AddSeconds(15);
    Console.WriteLine("Final myDateTime9 = " + myDateTime9);

  }
}
開發者ID:C#程序員,項目名稱:System,代碼行數:18,代碼來源:DateTime.AddYears


注:本文中的System.DateTime.AddYears方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。