w3resource logo
C# Sharp Programming Exercises

C# Sharp Exercises: Date of past and future fifteen years of a specified date

C# Sharp DateTime: Exercise-15 with Solution

Write a C# Sharp program to display the date of past and future fifteen years of a specified date.

Sample Solution:-

C# Sharp Code:

using System;
public class Example15
{
   public static void Main()
   {
      DateTime baseDate = new DateTime(2016, 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));
   }
}

Sample Output:

    Base Date:        2/29/2016                                                                               
 1 year(s) ago:        2/28/2015                                                                              
 2 year(s) ago:        2/28/2014                                                                              
 3 year(s) ago:        2/28/2013                                                                              
 4 year(s) ago:        2/29/2012                                                                              
 5 year(s) ago:        2/28/2011                                                                              
 6 year(s) ago:        2/28/2010                                                                              
 7 year(s) ago:        2/28/2009                                                                              
 8 year(s) ago:        2/29/2008                                                                              
 9 year(s) ago:        2/28/2007                                                                              
10 year(s) ago:        2/28/2006                                                                              
11 year(s) ago:        2/28/2005                                                                              
12 year(s) ago:        2/29/2004                                                                              
13 year(s) ago:        2/28/2003                                                                              
14 year(s) ago:        2/28/2002                                                                              
15 year(s) ago:        2/28/2001   
1 year(s) from now:   2/28/2017                                                                              
 2 year(s) from now:   2/28/2018                                                                              
 3 year(s) from now:   2/28/2019                                                                              
 4 year(s) from now:   2/29/2020                                                                              
 5 year(s) from now:   2/28/2021                                                                              
 6 year(s) from now:   2/28/2022                                                                              
 7 year(s) from now:   2/28/2023                                                                              
 8 year(s) from now:   2/29/2024                                                                              
 9 year(s) from now:   2/28/2025                                                                              
10 year(s) from now:   2/28/2026                                                                              
11 year(s) from now:   2/28/2027                                                                              
12 year(s) from now:   2/29/2028                                                                              
13 year(s) from now:   2/28/2029                                                                              
14 year(s) from now:   2/28/2030                                                                              
15 year(s) from now:   2/28/2031     

C# Sharp Code Editor:

using System;
public class Example15
{
   public static void Main()
   {
      DateTime baseDate = new DateTime(2016, 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));
   }
}

Improve this sample solution and post your code through Disqus