
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
Comparing dates using 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, 07, 20); Console.WriteLine("Date 1 : {0}", date1);
Date 2
DateTime date2 = new DateTime(2018, 07, 25); 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, 07, 20); Console.WriteLine("Date 1 : {0}", date1); DateTime date2 = new DateTime(2018, 07, 25); Console.WriteLine("Date 2 : {0}", date2); if (date1 < date2) Console.WriteLine("{0} comes before {1}", date1, date2); Console.Read(); return 0; } } }
Output
Date 1 : 7/20/2018 12:00:00 AM Date 2 : 7/25/2018 12:00:00 AM 7/20/2018 12:00:00 AM comes before 7/25/2018 12:00:00 AM
- Related Articles
- Comparing dates in Python
- Comparing two dates in PHP
- Comparing dates in MySQL ignoring time portion of a DateTime field?
- Comparing String objects using Relational Operators in C++
- Comparing ‘cubic’ and ‘linear’ 1-D interpolation using SciPy library
- PHP Comparing Objects
- Jasmine.js comparing arrays
- C++ program for Sorting Dates using Selection Sort
- Print dates of today, yesterday and tomorrow using Numpy
- Comparing Enumeration Values in Java
- Comparing two strings in MySQL?
- Comparing enum members in C#
- Comparing float value in PHP
- Comparing two strings in C++
- Comparing Timestamp in Python – Pandas

Advertisements