Q:

Write a C# Sharp program to compare the current date with a given date

0

Write a C# Sharp program to compare the current date with a given date. 

Expected Output :

7/28/2016 is in the past

All Answers

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

using System;

public class Example18
{
   public static void Main()
   {
      System.DateTime theDay = new System.DateTime(System.DateTime.Today.Year, 7, 28);
int compareValue;

try 
{
    compareValue = theDay.CompareTo(DateTime.Today);
} 
catch (ArgumentException) 
{
   Console.WriteLine("Value is not a DateTime");
   return;
}

if (compareValue < 0) 
   System.Console.WriteLine("{0:d} is in the past.", theDay);
else if (compareValue == 0) 
   System.Console.WriteLine("{0:d} is today!", theDay);
else // compareValue > 0
   System.Console.WriteLine("{0:d} has not come yet.", theDay);
   }
}

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