Those codes are Compare Two Dates in C# Console App. For comparing two dates we use DateTime class and if condution.
Example 1: Comparing two dates in C# Console
Screenshot:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | using System; //www.csharp-console-examples.com namespace CompareTwoDates { class Program { static void Main(string[] args) { DateTime date1 = DateTime.Parse("02/06/2019"); DateTime date2 = DateTime.Now; //Moment Console.WriteLine("Your Date :" + date1.ToShortDateString()); Console.WriteLine("Now :" + date2.ToShortDateString()); Console.WriteLine("**************************"); if (date1.Date > date2.Date) { //It's a later date Console.Write("It's a later date"); } else if(date1.Date < date2.Date) { //It's an earlier or equal date Console.Write("It's an earlier date "); } else { Console.Write("It's an equal date"); } Console.ReadLine(); } } } |
Compares two instances of DateTime and returns an integer that indicates whether the first instance is earlier than, the same as, or later than the second instance.
Example 2: Comparing two dates in the C# DataGridView
In this example shows us, how to compare two date in dataGridView using C# Windows Form Application.
Source Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace datagrid_1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { dataGridView1.ColumnCount = 3; dataGridView1.Columns[0].Name = "Date 1"; dataGridView1.Columns[1].Name = "Date 2"; dataGridView1.Columns[2].Name = "Comparison"; //dataGridView1.Columns[3].Name = "Time Span"; dataGridView1.Rows.Add("13.01.2018", "20.01.2018"); dataGridView1.Rows.Add("05.03.2018", "13.03.2018"); dataGridView1.Rows.Add("20.04.2018", "15.05.2018"); dataGridView1.Rows.Add("18.04.2018", "18.04.2018"); dataGridView1.Rows.Add("01.06.2018", "22.08.2018"); dataGridView1.Rows.Add("30.05.2018", "02.06.2017"); dataGridView1.Rows.Add("04.04.2018", "12.05.2018"); dataGridView1.Rows.Add("12.03.2018", "10.04.2018"); } private void button1_Click(object sender, EventArgs e) { DateTime d1; DateTime d2; for(int i=0;i<dataGridView1.RowCount-1;i++) { d1 = Convert.ToDateTime(dataGridView1.Rows[i].Cells[0].Value); d2 = Convert.ToDateTime(dataGridView1.Rows[i].Cells[1].Value); if(d1>d2) { dataGridView1.Rows[i].Cells[2].Value = "Date1 is greater than Date2."; } else if(d1==d2) { dataGridView1.Rows[i].Cells[2].Value = "Two dates are equal."; } else { dataGridView1.Rows[i].Cells[2].Value = "Date2 is greater than Date1."; } } } } } |
Output:
Example 3: Calculating Difference Between Two Dates in DataGridView Using C#
Source Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | private void Form1_Load(object sender, EventArgs e) { dataGridView1.ColumnCount = 4; dataGridView1.Columns[0].Name = "Book"; dataGridView1.Columns[1].Name = "Borrowed date"; dataGridView1.Columns[2].Name = "Delivered date"; dataGridView1.Columns[3].Name = "Time Span"; dataGridView1.Rows.Add("Book 1", "13.01.2018", "20.01.2018"); dataGridView1.Rows.Add("Book 2", "05.03.2018", "13.03.2018"); dataGridView1.Rows.Add("Book 3", "20.04.2018", "15.05.2018"); dataGridView1.Rows.Add("Book 4", "18.04.2018", "11.05.2018"); dataGridView1.Rows.Add("Book 5", "01.06.2018", "22.08.2018"); dataGridView1.Rows.Add("Book 6", "30.05.2018", "02.06.2018"); dataGridView1.Rows.Add("Book 7", "04.04.2018", "12.05.2018"); dataGridView1.Rows.Add("Book 8", "12.03.2018", "10.04.2018"); } private void button1_Click(object sender, EventArgs e) { DateTime d1; DateTime d2; for(int i=0;i<dataGridView1.RowCount-1;i++) { d1 = Convert.ToDateTime(dataGridView1.Rows[i].Cells[2].Value); d2 = Convert.ToDateTime(dataGridView1.Rows[i].Cells[1].Value); TimeSpan ts = d1 - d2; dataGridView1.Rows[i].Cells[3].Value = ts.Days; } } |
Output: