本文整理匯總了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));
}
}
輸出:
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);
}
}