C# Sharp Exercises: Compare the current date with a given date
C# Sharp DateTime: Exercise-18 with Solution
Write a C# Sharp program to compare the current date with a given date.
Sample Solution:-
C# Sharp Code:
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);
}
}
Sample Output:
7/28/2017 has not come yet.
Flowchart :

C# Sharp Code Editor:
Improve this sample solution and post your code through Disqus
Previous: 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.
Next: Write a C# Sharp program to get the number of days of the specified month and year.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Exercises: Weekly Top 12 Most Popular Topics
- Pandas DataFrame: Exercises, Practice, Solution
- Conversion Tools
- JavaScript: HTML Form Validation
- SQL Exercises, Practice, Solution - SUBQUERIES
- C Programming Exercises, Practice, Solution : For Loop
- Python Exercises, Practice, Solution
- Python Data Type: List - Exercises, Practice, Solution
- C++ Basic: Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - exercises on Employee Database
- SQL Exercises, Practice, Solution - exercises on Movie Database
- SQL Exercises, Practice, Solution - exercises on Soccer Database
- C Programming Exercises, Practice, Solution : Recursion