Q:

Write a C# Sharp Program to create a date one year previously and the date one year in the future compare to the current date

0

Write a C# Sharp Program to create a date one year previously and the date one year in the future compare to the current date. 

Expected Output :

1: 8/20/2016 is later than 8/20/2015                                             
-1: 8/20/2016 is earlier than 8/20/2017    

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

using System;

public class Example17
{
   private enum DateComparisonResult
   {
      Earlier = -1,
      Later = 1,
      TheSame = 0
   };

   public static void Main()
   {
      DateTime thisDate = DateTime.Today;
      DateTime thisDateNextYear, thisDateLastYear;
      //  add/substract 1 year
      thisDateNextYear = thisDate.AddYears(1);
      thisDateLastYear = thisDate.AddYears(-1);   
      DateComparisonResult comparison;
      // Compare today to last year
      comparison = (DateComparisonResult) thisDate.CompareTo(thisDateLastYear);
      Console.WriteLine("{0}: {1:d} is {2} than {3:d}", 
                        (int) comparison, thisDate, comparison.ToString().ToLower(), 
                        thisDateLastYear);
      // Compare today to next year
      comparison = (DateComparisonResult) thisDate.CompareTo(thisDateNextYear);
      Console.WriteLine("{0}: {1:d} is {2} than {3:d}", 
                        (int) comparison, thisDate, comparison.ToString().ToLower(), 
                        thisDateNextYear);
   }
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now