- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to compare two Dates in C#?
To compare dates in C#, you need to first set two dates to be compared using the DateTime object. We will use the DateTime class in C# −
Date 1
DateTime date1 = new DateTime(2018, 08, 05); Console.WriteLine("Date 1 : {0}", date1);
Date 2
DateTime date2 = new DateTime(2018, 08, 07); Console.WriteLine("Date 2 : {0}", date2);
Now let us compare both the dates in C#. The following is an example to compare dates in C# −
Example
using System; namespace Program { class Demo { static int Main() { DateTime date1 = new DateTime(2018, 08, 05); Console.WriteLine("Date 1 : {0}", date1); DateTime date2 = new DateTime(2018, 08, 07); Console.WriteLine("Date 2 : {0}", date2); if (date1 < date2) Console.WriteLine("{0} comes before {1}", date1, date2); Console.Read(); return 0; } } }
Advertisements